private void AccumulateOfficeRevenue(BrokerCommissionModal brokerDataModal, 
            string calculPeriodId, DateTime calculPeriod)
        {
            // Init office result data
            foreach (string officeID in brokerDataModal.HashOffice.Keys)
            {
                Office office = (Office)brokerDataModal.HashOffice[officeID];
                if (office != null)
                {
                    OfficeCalculationResult officeResult = new OfficeCalculationResult(office);
                    officeResult.PeriodId = calculPeriodId;
                    officeResult.CalculatePeriod = calculPeriod;
                    officeResult.TradingValue = 0;
                    officeResult.NetRevenue = 0;

                    office.Result = officeResult;
                }
            }

            // Accumulate broker data into offices
            foreach (string brkID in brokerDataModal.HashBroker.Keys)
            {
                Broker brk = (Broker)brokerDataModal.HashBroker[brkID];
                if ((brk != null) && (brk.Office != null) && (brk.Office.Id != null))
                {
                    if (brk.Office.Result != null)
                    {
                        brk.Office.Result.TradingValue += brk.PaymentResult.TradingValue;
                        brk.Office.Result.InheritedTradingValue += brk.PaymentResult.InheritedTradingValue;
                        brk.Office.Result.PrivateTradingValue += brk.PaymentResult.PrivateTradingValue;

                        brk.Office.Result.GrossRevenue += brk.PaymentResult.GrossRevenue;
                        brk.Office.Result.InheritedGrossRevenue += brk.PaymentResult.InheritedGrossRevenue;
                        brk.Office.Result.PrivateGrossRevenue += brk.PaymentResult.PrivateGrossRevenue;

                        brk.Office.Result.NetRevenue += brk.PaymentResult.NetRevenue;
                        brk.Office.Result.InheritedNetRevenue += brk.PaymentResult.InheritedNetRevenue;
                        brk.Office.Result.PrivateNetRevenue += brk.PaymentResult.PrivateNetRevenue;
                    }
                }
            }

            // Travel on each office to accumulate the branch data
            foreach (string officeID in brokerDataModal.HashOffice.Keys)
            {
                Office office = (Office)brokerDataModal.HashOffice[officeID];
                if (office != null)
                {
                    _logger.Info("Office " + office.Id + ": TradingValue=" + office.Result.TradingValue + " Revenue=" + office.Result.NetRevenue);

                    if ((office.Branch != null) && (office.Branch.Id != null))
                    {
                        if (office.Branch.Result == null)
                        {
                            office.Branch.Result = new BranchCalculationResult(office.Branch);
                            office.Branch.Result.PeriodId = calculPeriodId;
                            office.Branch.Result.CalculatePeriod = calculPeriod;
                        }
                        office.Branch.Result.TradingValue += office.Result.TradingValue;
                        office.Branch.Result.InheritedTradingValue += office.Result.InheritedTradingValue;
                        office.Branch.Result.PrivateTradingValue += office.Result.PrivateTradingValue;

                        office.Branch.Result.GrossRevenue += office.Result.GrossRevenue;
                        office.Branch.Result.InheritedGrossRevenue += office.Result.InheritedGrossRevenue;
                        office.Branch.Result.PrivateGrossRevenue += office.Result.PrivateGrossRevenue;

                        office.Branch.Result.NetRevenue += office.Result.NetRevenue;
                        office.Branch.Result.InheritedNetRevenue += office.Result.InheritedNetRevenue;
                        office.Branch.Result.PrivateNetRevenue += office.Result.PrivateNetRevenue;
                    }
                }
            }
        }
        public int InsertOfficeCalculationResultLog(OfficeCalculationResult rec, DateTime calculTime)
        {
            int affected = 0;
            string query = @"
                INSERT INTO OfficeCalculationResultLog
                    (PeriodId, 
                    CalculateTime, 
                    OfficeID, 
                    CalculatePeriod, 
                    TradingValue, 
                    InheritedTradingValue, 
                    PrivateTradingValue, 
                    GrossRevenue, 
                    InheritedGrossRevenue, 
                    PrivateGrossRevenue,
                    NetRevenue, 
                    InheritedNetRevenue, 
                    PrivateNetRevenue)
                VALUES
                    (@PeriodId, 
                    @CalculateTime, 
                    @OfficeID, 
                    @CalculatePeriod, 
                    @TradingValue, 
                    @InheritedTradingValue, 
                    @PrivateTradingValue, 
                    @GrossRevenue, 
                    @InheritedGrossRevenue, 
                    @PrivateGrossRevenue,
                    @NetRevenue, 
                    @InheritedNetRevenue, 
                    @PrivateNetRevenue)";
            ((SqlCommand)DataAccess.Command).Parameters.Clear();
            ((SqlCommand)DataAccess.Command).Parameters.Add("@PeriodId", SqlDbType.Char, 6);
            ((SqlCommand)DataAccess.Command).Parameters.Add("@CalculateTime", SqlDbType.DateTime);
            ((SqlCommand)DataAccess.Command).Parameters.Add("@OfficeID", SqlDbType.VarChar);
            ((SqlCommand)DataAccess.Command).Parameters.Add("@CalculatePeriod", SqlDbType.DateTime);
            ((SqlCommand)DataAccess.Command).Parameters.Add("@TradingValue", SqlDbType.Money);
            ((SqlCommand)DataAccess.Command).Parameters.Add("@InheritedTradingValue", SqlDbType.Money);
            ((SqlCommand)DataAccess.Command).Parameters.Add("@PrivateTradingValue", SqlDbType.Money);
            ((SqlCommand)DataAccess.Command).Parameters.Add("@GrossRevenue", SqlDbType.Money);
            ((SqlCommand)DataAccess.Command).Parameters.Add("@InheritedGrossRevenue", SqlDbType.Money);
            ((SqlCommand)DataAccess.Command).Parameters.Add("@PrivateGrossRevenue", SqlDbType.Money);
            ((SqlCommand)DataAccess.Command).Parameters.Add("@NetRevenue", SqlDbType.Money);
            ((SqlCommand)DataAccess.Command).Parameters.Add("@InheritedNetRevenue", SqlDbType.Money);
            ((SqlCommand)DataAccess.Command).Parameters.Add("@PrivateNetRevenue", SqlDbType.Money);

            ((SqlCommand)DataAccess.Command).Parameters["@PeriodId"].Value = rec.PeriodId;
            ((SqlCommand)DataAccess.Command).Parameters["@CalculateTime"].Value = calculTime;
            ((SqlCommand)DataAccess.Command).Parameters["@OfficeID"].Value = rec.Office.Id;
            ((SqlCommand)DataAccess.Command).Parameters["@CalculatePeriod"].Value = rec.CalculatePeriod;
            ((SqlCommand)DataAccess.Command).Parameters["@TradingValue"].Value = rec.TradingValue;
            ((SqlCommand)DataAccess.Command).Parameters["@InheritedTradingValue"].Value = rec.InheritedTradingValue;
            ((SqlCommand)DataAccess.Command).Parameters["@PrivateTradingValue"].Value = rec.PrivateTradingValue;
            ((SqlCommand)DataAccess.Command).Parameters["@GrossRevenue"].Value = rec.GrossRevenue;
            ((SqlCommand)DataAccess.Command).Parameters["@InheritedGrossRevenue"].Value = rec.InheritedGrossRevenue;
            ((SqlCommand)DataAccess.Command).Parameters["@PrivateGrossRevenue"].Value = rec.PrivateGrossRevenue;
            ((SqlCommand)DataAccess.Command).Parameters["@NetRevenue"].Value = rec.NetRevenue;
            ((SqlCommand)DataAccess.Command).Parameters["@InheritedNetRevenue"].Value = rec.InheritedNetRevenue;
            ((SqlCommand)DataAccess.Command).Parameters["@PrivateNetRevenue"].Value = rec.PrivateNetRevenue;

            affected = DataAccess.ExecuteNonQuery(query);

            return affected;
        }