private void CalculateSupervisor(Broker brk, BrokerCommissionModal masterData)
        {
            // Calculate the total of all subordinates
            foreach (string key in brk.HashSubordinate.Keys)
            {
                Broker subBrk = (Broker)brk.HashSubordinate[key];

                brk.PaymentResult.SubtotalNetRevenue += subBrk.PaymentResult.NetRevenue;
                brk.PaymentResult.SubtotalInheritedNetRevenue += subBrk.PaymentResult.InheritedNetRevenue;
                brk.PaymentResult.SubtotalPrivateNetRevenue += subBrk.PaymentResult.PrivateNetRevenue;

                brk.PaymentResult.SubtotalTradingValue += subBrk.PaymentResult.TradingValue;
                brk.PaymentResult.SubtotalInheritedTradingValue += subBrk.PaymentResult.InheritedTradingValue;
                brk.PaymentResult.SubtotalPrivateTradingValue += subBrk.PaymentResult.PrivateTradingValue;
            }

            // Find the Commission Rates
            // Find the Commission Rate for ClientType = 1 --> Client from HSC
            CommissionRate rate4OldClient = masterData.FindCommissionRate(brk.SUPCommissionCode,
                ClientType.CLIENTTYPE_ID_FROMHSC, brk.PaymentResult.SubtotalNetRevenue);

            // Find the Commission Rate for ClientType = 2 --> Client found by broker
            CommissionRate rate4NewClient = masterData.FindCommissionRate(brk.SUPCommissionCode,
                    ClientType.CLIENTTYPE_ID_BYBROKER, brk.PaymentResult.SubtotalNetRevenue);

            if (rate4NewClient == null)
            {
                _logger.Error("CalculateSupervisor - rate4NewClient not found. BrokerID=" + brk.Id);
                rate4NewClient = new CommissionRate(ClientType.CLIENTTYPE_ID_BYBROKER, Constants.BROKERTYPE_SUP);
            }

            if (rate4OldClient == null)
            {
                _logger.Error("CalculateSupervisor - rate4OldClient not found. BrokerID=" + brk.Id);
                rate4OldClient = new CommissionRate(ClientType.CLIENTTYPE_ID_FROMHSC, Constants.BROKERTYPE_SUP);
            }

            Decimal a = (brk.PaymentResult.SubtotalInheritedNetRevenue * (decimal)rate4OldClient.Commissionrate); // A
            Decimal b = (brk.PaymentResult.SubtotalPrivateNetRevenue * (decimal)rate4NewClient.Commissionrate); // B1 or B2 depends on the matched rate
            brk.PaymentResult.SupervisorPayment = a + b;
            brk.PaymentResult.Payment += brk.PaymentResult.SupervisorPayment;
            //_logger.Debug("CalculateSupervisor - LEAVE");
        }
        private void CalculateManager(Broker brk, BrokerCommissionModal masterData)
        {
            //_logger.Debug("CalculateManager - ENTER");
            if ((brk.Office == null) || (brk.Office.Id == null) || (brk.Office.Branch == null)) return;

            decimal revenue = 0;
            decimal value = 0;
            decimal value4FindingRate = 0;
            if (brk.Office.IsBranch(Office.BRANCH_ID_HN))
            {
                revenue = brk.Office.Branch.Result.NetRevenue;
                value = brk.Office.Branch.Result.TradingValue;
                value4FindingRate = value;
            }
            else if (brk.Office.IsBranch(Office.BRANCH_ID_HCM))
            {
                revenue = brk.Office.Result.NetRevenue;
                value = brk.Office.Result.TradingValue;
                value4FindingRate = (value - 1000000000000); // 1K Bil
                if (value4FindingRate < 0) value4FindingRate = 0;
            }

            CommissionRate rate4Manager = masterData.FindCommissionRate(brk.MANCommissionCode,
                ClientType.CLIENTTYPE_ID_FROMHSC, value4FindingRate);

            if (rate4Manager == null)
            {
                _logger.Error("CalculateManager - rate4OldClient not found. BrokerID=" + brk.Id);
                rate4Manager = new CommissionRate(ClientType.CLIENTTYPE_ID_FROMHSC, Constants.BROKERTYPE_MAN);
            }

            brk.PaymentResult.ManagementBonusRate = rate4Manager.Commissionrate;
            brk.PaymentResult.SupervisorPayment = (revenue * (decimal)rate4Manager.Commissionrate);
            if (brk.AdjustRate != null)
            {
                brk.PaymentResult.SupervisorPayment = (brk.PaymentResult.SupervisorPayment * ((decimal)brk.AdjustRate.AdjustRate));
            }

            brk.PaymentResult.Payment += brk.PaymentResult.SupervisorPayment;

            //_logger.Debug("CalculateManager - LEAVE");
        }
        private void CalculateAE(Broker brk, string calculPeriodId, BrokerCommissionModal masterData)
        {   
            // prepare data for Calculation
            PrepareTradingData(brk);

            BrokerCalculationResult calculResult = brk.PaymentResult;
            if (calculResult.NetRevenue < NEW_CALCULATION_LIMIT) //using OLD METHOD
            #region OLD METHOD
            {
                // Find the Commission Rate for OLD CLIENTS
                CommissionRate hscClientRate = masterData.FindCommissionRate(brk.AECommissionCode,
                        ClientType.CLIENTTYPE_ID_FROMHSC, calculResult.NetRevenue);
                if (hscClientRate != null)
                {
                    calculResult.InheritedCommissionRate = hscClientRate.Commissionrate;
                }
                else
                {
                    calculResult.InheritedCommissionRate = 0;
                }
                // Commission payment for hsc clients
                calculResult.InheritedCommissionPayment = (calculResult.InheritedNetRevenue * (decimal)calculResult.InheritedCommissionRate);

                // Find the Commission Rate for New clients
                CommissionRate aeClientRate = masterData.FindCommissionRate(brk.AECommissionCode,
                        ClientType.CLIENTTYPE_ID_BYBROKER, calculResult.NetRevenue);

                if (aeClientRate != null)
                {
                    calculResult.PrivateCommissionRate = aeClientRate.Commissionrate;
                    calculResult.PrivateCommissionRate1 = aeClientRate.Commissionrate;
                    calculResult.PrivateCommissionRate2 = 0;
                }
                else
                {
                    calculResult.PrivateCommissionRate = 0;
                    calculResult.PrivateCommissionRate1 = 0;
                    calculResult.PrivateCommissionRate2 = 0;
                }

                // Commission payment for hsc clients
                calculResult.PrivateCommissionPayment = (calculResult.PrivateNetRevenue * (decimal)calculResult.PrivateCommissionRate);
                calculResult.OtherPayment = 0;
                if (brk.IsAccountExecutive())
                {
                    AllowanceDTO allowanceRate = masterData.GetAllowance(brk.AECommissionCode, calculResult.NetRevenue);
                    if (allowanceRate != null)
                    {
                        calculResult.OtherPayment = allowanceRate.Allowance;
                    }
                }

                // Total payment
                calculResult.Payment += calculResult.InheritedCommissionPayment + calculResult.PrivateCommissionPayment + calculResult.OtherPayment;
            }
            #endregion
            else //using NEW METHOD
            #region NEW METHOD
            {
                // Find the Commission Rate for OLD CLIENTS
                CommissionRate1 hscClientRate = masterData.FindCommissionRate1(brk.AECommissionCode,
                        ClientType.CLIENTTYPE_ID_FROMHSC, calculResult.NetRevenue);
                if (hscClientRate != null)
                {
                    calculResult.InheritedCommissionRate = hscClientRate.LowerRate;
                }
                else
                {
                    calculResult.InheritedCommissionRate = 0;
                }
                // Commission payment for hsc clients
                calculResult.InheritedCommissionPayment = (calculResult.InheritedNetRevenue * (decimal)calculResult.InheritedCommissionRate);
                
                // Find the Commission Rate for New clients
                CommissionRate1 aeClientRate = masterData.FindCommissionRate1(brk.AECommissionCode,
                        ClientType.CLIENTTYPE_ID_BYBROKER, calculResult.NetRevenue);

                calculResult.PrivateCommissionPayment = 0;
                if (aeClientRate != null)
                {
                    calculResult.PrivateCommissionRate = aeClientRate.LowerRate;
                    calculResult.PrivateCommissionRate1 = aeClientRate.LowerRate;
                    calculResult.PrivateCommissionRate2 = aeClientRate.UpperRate;
                    if (calculResult.PrivateNetRevenue < aeClientRate.Limit)
                    {
                        calculResult.PrivateCommissionPayment = (decimal)aeClientRate.LowerRate * calculResult.PrivateNetRevenue;
                    }
                    else
                    {
                        calculResult.PrivateCommissionPayment = (decimal)aeClientRate.LowerRate * aeClientRate.Limit + (calculResult.PrivateNetRevenue - aeClientRate.Limit) * (decimal)aeClientRate.UpperRate;
                    }
                }
                else
                {
                    calculResult.PrivateCommissionRate = 0;
                    calculResult.PrivateCommissionRate1 = 0;
                    calculResult.PrivateCommissionRate2 = 0;
                }
                                              
                calculResult.OtherPayment = 0;
                if (brk.IsAccountExecutive())
                {
                    AllowanceDTO allowanceRate = masterData.GetAllowance(brk.AECommissionCode, calculResult.NetRevenue);
                    if (allowanceRate != null)
                    {
                        calculResult.OtherPayment = allowanceRate.Allowance;
                    }
                }
                // Total payment
                calculResult.Payment += calculResult.InheritedCommissionPayment + calculResult.PrivateCommissionPayment + calculResult.OtherPayment;

            }
            #endregion
        }