public bool IsWeeklyConfirm(int periodID, int entityID)
        {
            WeeklySummaryCollection collection = new WeeklySummaryCollection();
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand();
                command.Connection = connection;
                command.CommandText = "SP_SEL_Table";
                SqlParameter _param = command.Parameters.Add("@value1", System.Data.SqlDbType.VarChar);
                _param.Value = "Weekly_Summary";
                SqlParameter _param2 = command.Parameters.Add("@value2", System.Data.SqlDbType.VarChar);
                _param2.Value = "1,2";
                SqlParameter _param3 = command.Parameters.Add("@value3", System.Data.SqlDbType.VarChar);
                _param3.Value = periodID + "," + entityID;
                SqlParameter _param4 = command.Parameters.Add("@order_by1", System.Data.SqlDbType.VarChar);
                _param4.Value = "1";
                SqlParameter _param5 = command.Parameters.Add("@order_by2", System.Data.SqlDbType.TinyInt);
                _param5.Value = 0;
                command.CommandType = System.Data.CommandType.StoredProcedure;
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        WeeklySummary _week = new WeeklySummary();
                        PeriodAccess _perioda = new PeriodAccess();
                        _week.Period = new PeriodCollection(_perioda.Query(Convert.ToInt32(reader["Period_ID"])))[0]; //_periodc[0];

                        EntityAccess _entity = new EntityAccess();
                        _week.Entity = new EntityCollection(_entity.Query(Convert.ToInt32(reader["Entity_Id"])))[0];
                        _week.BaseCurrency = reader["Base_Currency"].ToString();
                        _week.ExchangeRate = Convert.ToDecimal(reader["Exchange_Rate"].ToString());
                        _week.BasePrevBalance = Convert.ToDecimal(reader["Base_Prev_Balance"].ToString());
                        _week.SGDPrevBalance = Convert.ToDecimal(reader["SGD_Prev_Balance"].ToString());
                        _week.BaseWinAndLoss = Convert.ToDecimal(reader["Base_Win_Lose"].ToString());
                        _week.SGDWinAndLoss = Convert.ToDecimal(reader["SGD_Win_Lose"].ToString());
                        _week.BaseTransfer = Convert.ToDecimal(reader["Base_Transfer"].ToString());
                        _week.SGDTransfer = Convert.ToDecimal(reader["SGD_Transfer"].ToString());
                        _week.BaseTransaction = Convert.ToDecimal(reader["Base_Transaction"].ToString());
                        _week.SGDTransaction = Convert.ToDecimal(reader["SGD_Transaction"].ToString());
                        _week.BaseBalance = Convert.ToDecimal(reader["Base_Balance"].ToString());
                        _week.SGDBalance = Convert.ToDecimal(reader["SGD_Balance"].ToString());
                        _week.Status = (WeeklySummaryStatus)Convert.ToInt32(reader["Status"].ToString());
                        string userinfo = reader["Confirm_User"].ToString();
                        try
                        {
                            UserAccess usera = new UserAccess();
                            _week.ConfirmUser = new UserCollection(usera.Query(Convert.ToInt32(reader["Confirm_User"])))[0];
                        }
                        catch
                        {
                            _week.ConfirmUser = new User();
                        }
                        collection.Add(_week);
                    }
                    reader.Close();
                    if (collection.Count > 0)
                    {
                        if (collection[0].ConfirmUser.UserID.Equals(0))
                            return false;
                        else
                            return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    reader.Close();
                    return false;
                }
            }
        }
        public void SetConfirm(Transaction transaction)
        {
            using (TransactionAccessClient _tran = new TransactionAccessClient(EndpointName.TransactionAccess))
            {
                SettleService _settle = new SettleService();
                WeeklySummaryCollection _weekco = new WeeklySummaryCollection();
                //From Entity
                WeeklySummary _weekFrom = new WeeklySummary();
                _weekFrom.ExchangeRate = transaction.ExchangeRate;
                _weekFrom.Period = transaction.Period;
                _weekFrom.Entity = transaction.FromEntity;
                _weekFrom.BaseTransaction = -(transaction.Amount);
                _weekFrom.BaseCurrency = transaction.FromCurrency;
                //To Entity
                WeeklySummary _weekTo = new WeeklySummary();
                _weekTo.ExchangeRate = transaction.ExchangeRate;
                _weekTo.Period = transaction.Period;
                _weekTo.Entity = transaction.ToEntity;
                _weekTo.BaseTransaction = transaction.To_Amount;
                _weekTo.BaseCurrency = transaction.ToCurrency;
                _weekco.Add(_weekFrom);
                _weekco.Add(_weekTo);

                _settle.TransactionConfirm(_weekco);

                _tran.SetConfirm(transaction.ID, transaction.Updater.UserID, transaction.Period.ID);
            }
        }
        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 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 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;
        }