/// <summary>
        /// 
        /// </summary>
        /// <param name="i"></param>
        /// <param name="isEnable"></param>
        /// <param name="smtpServer"></param>
        /// <param name="smtpLogin"></param>
        /// <param name="smtpPassword"></param>
        internal Model.MailConfig GetMailConfig(Business.Investor objInvestor)
        {
            Model.MailConfig result = new Model.MailConfig();

            #region GET PARAMETER SEND MAIL IN GROUP OF INVESTOR
            bool isEnable = false;
            string smtpServer = string.Empty;
            string smtpLogin = string.Empty;
            string smtpPassword = string.Empty;
            string supportEmail = string.Empty;
            string signature = string.Empty;

            if (objInvestor.InvestorGroupInstance != null)
            {
                if (objInvestor.InvestorGroupInstance.ParameterItems != null)
                {
                    int countParameter = objInvestor.InvestorGroupInstance.ParameterItems.Count;
                    for (int j = 0; j < countParameter; j++)
                    {
                        if (objInvestor.InvestorGroupInstance.ParameterItems[j].Code == "G29")
                        {
                            if (objInvestor.InvestorGroupInstance.ParameterItems[j].BoolValue == 1)
                                isEnable = true;
                        }

                        if (objInvestor.InvestorGroupInstance.ParameterItems[j].Code == "G30")
                        {
                            smtpServer = objInvestor.InvestorGroupInstance.ParameterItems[j].StringValue;
                        }

                        if (objInvestor.InvestorGroupInstance.ParameterItems[j].Code == "G32")
                        {
                            smtpLogin = objInvestor.InvestorGroupInstance.ParameterItems[j].StringValue;
                        }

                        if (objInvestor.InvestorGroupInstance.ParameterItems[j].Code == "G33")
                        {
                            smtpPassword = objInvestor.InvestorGroupInstance.ParameterItems[j].StringValue;
                        }

                        if (objInvestor.InvestorGroupInstance.ParameterItems[j].Code == "G34")
                        {
                            supportEmail = objInvestor.InvestorGroupInstance.ParameterItems[j].StringValue;
                        }

                        if (objInvestor.InvestorGroupInstance.ParameterItems[j].Code == "G35")
                        {
                            signature = objInvestor.InvestorGroupInstance.ParameterItems[j].StringValue;
                        }
                    }
                }
            }
            #endregion

            #region SET MAIL CONFIG
            result.isEnable = isEnable;
            result.DisplayNameFrom = "Element 5";
            result.MessageFrom = smtpLogin;
            result.PasswordCredential = smtpPassword;
            result.SmtpHost = smtpServer;
            result.UserCredential = smtpLogin;
            result.Signature = signature;
            #endregion

            return result;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 
        /// </summary>
        internal void ReCalculationAccount()
        {
            this.IsCalculating = true;

            if (this.CommandList != null)
            {
                if (this.CommandList.Count > 0 && this.CommandList != null)
                {
                    //Call function Total Margin Of Investor
                    //this.Margin = this.CommandList[0].Symbol.CalculationTotalMargin(this.CommandList);
                    this.Profit = this.InvestorGroupInstance.CalculationTotalProfit(this.CommandList);
                    this.Equity = this.Balance + this.Credit + this.Profit;
                    double Loss = 0;
                    double Profit = 0;
                    int Method = -1;

                    switch (this.InvestorGroupInstance.FreeMargin)
                    {
                        case "do not use unrealized profit/loss":
                            Method = 0;
                            break;
                        case "use unrealized profit/loss":
                            Method = 1;
                            break;
                        case "use unrealized profit only":
                            Method = 2;
                            Profit = this.InvestorGroupInstance.CalculationTotalProfitPositive(this.CommandList);
                            break;
                        case "use unrealized loss only":
                            Method = 3;
                            Loss = this.InvestorGroupInstance.CalculationTotalLoss(this.CommandList);
                            break;
                    }

                    double totalMargin = this.Margin + this.FreezeMargin;
                    this.FreeMargin = this.InvestorGroupInstance.CalculationTotalFreeMargin(totalMargin, this.Balance, this.Equity, Profit, Loss, Method);
                    this.MarginLevel = (this.Equity * 100) / (this.Margin + this.FreezeMargin);
                }
                else
                {
                    this.Margin = 0;
                    this.FreeMargin = 0;
                    this.FreezeMargin = 0;
                    this.MarginLevel = 0;
                    this.Profit = 0;
                }
            }

            try
            {
                if (this.CommandList.Count > 0)
                {
                    if (this.MarginLevel <= this.InvestorGroupInstance.MarginCall)
                    {
                        StringBuilder content = new StringBuilder();
                        content.Append("html");
                        content.Append("<body style='font-family: sans-serif'>");
                        content.Append("<div style='font-size: 15px; color: White; background-color: #BABABA; height: 17px;border-radius:3px;padding: 10px'>MARGIN CALL ALERT</div>");
                        content.Append("<div style='font-size: 12px; color: black; background-color: #E3E3E3;margin-top: 10px; padding: 15px;border-radius:3px;'>Dear Client,<br/>");
                        content.Append("<br/>Your account ");
                        content.Append(this.Code + " ");
                        content.Append("has just reached the margin call level at ");

                        content.Append(DateTime.Now.Hour + ":" + DateTime.Now.Minute +
                                        " [" + DateTime.Now.Day + "/" + DateTime.Now.Month +
                                        "/" + DateTime.Now.Year + "]");

                        content.Append(".<br/> Margin Level is now ");
                        content.Append(Math.Round(this.MarginLevel, 2));
                        content.Append("%.<br/>");
                        content.Append("<br/>Best Regards,<br/>Customer Service</div>");
                        content.Append("</body>");
                        content.Append("</html>");

                        //SEND MAIL IF ACCOUNT MARGIN CALL
                        TimeSpan timeSpan = DateTime.Now - this.TimeWarningMarginCall;
                        if (timeSpan.TotalHours > Business.Market.TimeDelaySendWarningMarginCall)
                        {
                            this.TimeWarningMarginCall = DateTime.Now;

                            Model.MailConfig newMailConfig = new Model.MailConfig();
                            newMailConfig = Business.Market.marketInstance.GetMailConfig(this);

                            #region GET MAIL CONFIG AND SEND REPORT
                            if (newMailConfig != null)
                            {
                                if (newMailConfig.isEnable)
                                {
                                    if (!string.IsNullOrEmpty(newMailConfig.SmtpHost) && !string.IsNullOrEmpty(newMailConfig.MessageFrom) &&
                                        !string.IsNullOrEmpty(newMailConfig.PasswordCredential))
                                    {
                                        if (!string.IsNullOrEmpty(this.Email))
                                        {
                                            Model.TradingCalculate.Instance.SendMailAsync(this.Email, "MARGIN CALL ALERT", content.ToString(),
                                                                                            newMailConfig);
                                        }
                                    }
                                }
                            }
                            #endregion

                            string logSendMail = string.Empty;
                            logSendMail = "Send Mail Alert Margin Call Account: " + this.Code;
                            TradingServer.Facade.FacadeAddNewSystemLog(1, logSendMail, "[Alert Margin Call]", "", this.Code);
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }

            //Check Stop Out Level Of Account
            if (this.MarginLevel <= this.InvestorGroupInstance.MarginStopOut)
            {
                //string content = "stop out account " + this.Code + " " + Math.Round(this.MarginLevel, 2);
                //TradingServer.Facade.FacadeAddNewSystemLog(5, content, "[stop out account] " + this.Code, "", this.Code);
                //Call Function Close Command

                if (this.CommandList != null && this.CommandList.Count > 0)
                {
                    bool isManualStopOut = TradingServer.Facade.FacadeCheckManaulStopOut(this.InvestorGroupInstance.InvestorGroupID);
                    if (!isManualStopOut)
                    {
                        bool result = this.ReCheckStopOutAccount();
                        if (result)
                        {
                            this.StopOutLevel();
                        }
                    }
                }
            }

            this.IsCalculating = false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="targetName"></param>
        /// <param name="timeStartDay"></param>
        /// <param name="timeEndDay"></param>
        internal void SendReportDayManaual(string targetName, DateTime timeStartDay, DateTime timeEndDay)
        {
            return;

            DateTime tempTimeStart = timeStartDay;
            DateTime tempTimeEnd = timeEndDay;

            timeStartDay = new DateTime(tempTimeStart.Year, tempTimeStart.Month, tempTimeStart.Day, 00, 00, 00);
            timeEndDay = new DateTime(tempTimeEnd.Year, tempTimeEnd.Month, tempTimeEnd.Day, 23, 59, 59);

            string pathStatement = string.Empty;
            if (Business.Market.MarketConfig != null)
            {
                int countMarketConfig = Business.Market.MarketConfig.Count;
                for (int i = 0; i < countMarketConfig; i++)
                {
                    if (Business.Market.MarketConfig[i].Code == "C35")
                    {
                        pathStatement = Business.Market.MarketConfig[i].StringValue;
                        break;
                    }
                }
            }

            #region end html
            string content = string.Empty;
            string begin = string.Empty;
            string header = "<div align=center style='font: 20pt Times New Roman'><b>PT Millennium Penata Futures ET5 Demo Account</b></div></br>";            
            string endString = "</body></html>";
            #endregion  

            #region SEND MAIL TO INVESTOR
            if (Business.Market.InvestorList != null && Business.Market.InvestorList.Count > 0)
            {
                int count = Business.Market.InvestorList.Count;
                for (int i = 0; i < count; i++)
                {                    
                    if (Business.Market.InvestorList[i].SendReport)
                    {
                        List<Business.OpenTrade> listOpenTrade = new List<OpenTrade>();
                        List<Business.OpenTrade> listPendingOrder = new List<OpenTrade>();
                        List<Business.OpenTrade> listCommandHistory = new List<OpenTrade>();

                        #region GET ONLINE COMMAND OF INVESTOR
                        if (Business.Market.CommandExecutor != null)
                        {
                            int countCommand = Business.Market.CommandExecutor.Count;
                            for (int j = 0; j < countCommand; j++)
                            {
                                if (Business.Market.CommandExecutor[j].Investor.InvestorID == Business.Market.InvestorList[i].InvestorID)
                                {
                                    bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Business.Market.CommandExecutor[j].Type.ID);
                                    if (!isPending)
                                    {
                                        double tempProfit = Math.Round(Business.Market.CommandExecutor[j].Profit, 2);
                                        Business.Market.CommandExecutor[j].Profit = tempProfit;
                                        listOpenTrade.Add(Business.Market.CommandExecutor[j]);
                                    }
                                    else
                                    {
                                        double tempProfit = Math.Round(Business.Market.CommandExecutor[j].Profit, 2);
                                        Business.Market.CommandExecutor[j].Profit = tempProfit;

                                        listPendingOrder.Add(Business.Market.CommandExecutor[j]);
                                    }
                                }
                            }
                        }
                        #endregion

                        #region GET COMMAND HISTORY OF INVESTOR
                        listCommandHistory = TradingServer.Facade.FacadeGetCommandHistoryWithTime(Business.Market.InvestorList[i].InvestorID, timeStartDay, timeEndDay);
                        #endregion

                        Model.MailConfig newMailConfig = new Model.MailConfig();
                        newMailConfig = this.GetMailConfig(Business.Market.InvestorList[i]);

                        #region DATA > 0
                        if (listOpenTrade != null && listOpenTrade.Count > 0 ||
                          listPendingOrder != null && listPendingOrder.Count > 0 ||
                          listCommandHistory != null && listCommandHistory.Count > 0)
                        {
                            #region REND ACCOUNT HTML
                            StringBuilder headerAccount = new StringBuilder();
                            headerAccount.Append("<tr>");
                            headerAccount.Append("<td align='right'>");
                            headerAccount.Append("<div class='statementHeader'>PT. Millennium Penata Futures ET5 Demo Account</div>");
                            headerAccount.Append("</td>");
                            headerAccount.Append("</tr>");

                            headerAccount.Append("<tr align='left'>");
                            headerAccount.Append("<td>");
                            headerAccount.Append("<table style='width: 100%;border-top:1px black solid;border-bottom:1px black solid;margin-top:20px;padding:0px'>");
                            headerAccount.Append("<tr style='background-color:#EEEEEE;'>");
                            headerAccount.Append("<td style='width: auto;font-weight:bolder;'>Account</td>");
                            headerAccount.Append("<td style='width: auto;font-weight:bolder;'>Name</td>");
                            headerAccount.Append("<td style='width: auto;font-weight:bolder;'>Currency</td>");
                            headerAccount.Append("<td style='width: auto;font-weight:bolder;'>Date</td>");

                            headerAccount.Append("<tr>");
                            headerAccount.Append("<td style='width: auto'>");
                            headerAccount.Append(Business.Market.InvestorList[i].Code + "</td>");
                            headerAccount.Append("<td style='width: auto'>");
                            headerAccount.Append(Business.Market.InvestorList[i].NickName + "</td>");
                            headerAccount.Append("<td style='width: auto'>USD</td>");
                            headerAccount.Append("<td style='width: auto'>");
                            headerAccount.Append(timeEndDay.ToShortDateString());
                            headerAccount.Append("</td>");
                            headerAccount.Append("</tr>");
                            headerAccount.Append("</table>");
                            headerAccount.Append("</td>");
                            headerAccount.Append("</tr>");
                            #endregion
                            
                            Business.ClientReport newClientReport = new ClientReport();
                            
                            string tempHeaderHTML = newClientReport.RendStyleStatement();
                            string tempResult = newClientReport.RendStatement(Business.Market.InvestorList[i], listCommandHistory, listOpenTrade, listPendingOrder);                            

                            string signature = string.Empty;
                            if (!string.IsNullOrEmpty(newMailConfig.Signature))
                            {
                                signature = "<tr><td align='right'>" + newMailConfig.Signature + "</td></tr>";
                                signature += "</table>";
                            }
                                                        
                            content = tempHeaderHTML + headerAccount + tempResult + signature + endString;

                            if (newMailConfig != null)
                            {
                                if (newMailConfig.isEnable)
                                {
                                    if (!string.IsNullOrEmpty(newMailConfig.SmtpHost) && !string.IsNullOrEmpty(newMailConfig.MessageFrom) &&
                                        !string.IsNullOrEmpty(newMailConfig.PasswordCredential))
                                    {
                                        if (!string.IsNullOrEmpty(Business.Market.InvestorList[i].Email))
                                        {
                                            Model.TradingCalculate.Instance.SendMail(Business.Market.InvestorList[i].Email, "Daily Confirmation", content, newMailConfig);

                                            if (!string.IsNullOrEmpty(pathStatement) && pathStatement != "NaN")
                                            {
                                                //STREAM FILE SAVE CONTENT SEND MAIL REPORT
                                                TradingServer.Model.TradingCalculate.Instance.StreamFile(content, Business.Market.InvestorList[i].Code, timeStartDay, 0, pathStatement);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        #endregion
                    }

                    #region UPDATE PREVIOUS LEDGER BALANCE
                    //TradingServer.Facade.FacadeUpdatePreviousLedgerBalance(Business.Market.InvestorList[i].InvestorID, preBalance);
                    #endregion                     
                }
            }
            #endregion            
        }
        /// <summary>
        /// SEND REPORT IN MONTH
        /// </summary>
        /// <param name="TargetName"></param>
        internal void SendReportMonth(string TargetName, Business.TimeEvent timeEvent)
        {
            return;

            #region GET END OF DAY
            string endOfDay = string.Empty;
            DateTime timeCurrent = DateTime.Now;
            DateTime timeEndDay = new DateTime();
            DateTime timeStartDay = new DateTime();
            bool IsReportWeekend = false;
            string pathStatement = string.Empty;
            if (Business.Market.MarketConfig != null)
            {
                int count = Business.Market.MarketConfig.Count;
                for (int i = 0; i < count; i++)
                {
                    if (Business.Market.MarketConfig[i].Code == "C12")
                    {
                        endOfDay = Business.Market.MarketConfig[i].StringValue;
                    }

                    if (Business.Market.MarketConfig[i].Code == "C26")
                    {
                        if (Business.Market.MarketConfig[i].BoolValue == 1)
                            IsReportWeekend = true;
                    }

                    if (Business.Market.MarketConfig[i].Code == "C35")
                    {
                        pathStatement = Business.Market.MarketConfig[i].StringValue;
                    }
                }
            }

            if (DateTime.Now.Hour < 10)
                timeCurrent = timeCurrent.AddDays(-1);

            timeEndDay = new DateTime(timeCurrent.Year, timeCurrent.Month, timeCurrent.Day, 23, 59, 59);
            timeStartDay = new DateTime(timeCurrent.Year, timeCurrent.Month, 1, 00, 00, 00);

            #region COMMEND CODE BECAUSE DON'T NEED CHECK END OF DAY ALLWAY LAST DAY OF MONTH(03/05/2013->DD/MM/YYYY)
            //switch (endOfDay)
            //{
            //    case "Last day of month":
            //        {
            //            if (DateTime.Now.Hour < 10)
            //                timeCurrent = timeCurrent.AddDays(-1);

            //            timeEndDay = new DateTime(timeCurrent.Year, timeCurrent.Month, timeCurrent.Day, 23, 59, 59);
            //            timeStartDay = new DateTime(timeCurrent.Year, timeCurrent.Month, 1, 00, 00, 00);
            //        }
            //        break;
            //    case "First day of month":
            //        {
            //            if (DateTime.Now.Hour > 10)
            //                timeCurrent = timeCurrent.AddDays(1);

            //            timeCurrent = timeCurrent.AddMonths(-1);

            //            timeEndDay = new DateTime(timeCurrent.Year, timeCurrent.Month, timeCurrent.Day, 0, 0, 0);
            //            timeStartDay = new DateTime(timeCurrent.Year, timeCurrent.Month, 1, 00, 00, 00);
            //        }
            //        break;
            //}
            #endregion
            
            #endregion

            #region CHECK Generate statements at weekends
            if (timeStartDay.DayOfWeek == DayOfWeek.Sunday || timeStartDay.DayOfWeek == DayOfWeek.Saturday)
            {
                if (!IsReportWeekend)
                    return;
            }
            #endregion

            #region SEND MAIL TO INVESTOR
            if (Business.Market.InvestorList != null && Business.Market.InvestorList.Count > 0)
            {
                int count = Business.Market.InvestorList.Count;
                for (int i = 0; i < count; i++)
                {
                    List<Business.OpenTrade> listOpenTrade = new List<OpenTrade>();
                    List<Business.OpenTrade> listPendingOrder = new List<OpenTrade>();
                    List<Business.OpenTrade> listCommandHistory = new List<OpenTrade>();

                    #region GET ONLINE COMMAND OF INVESTOR
                    if (Business.Market.CommandExecutor != null)
                    {
                        int countCommand = Business.Market.CommandExecutor.Count;
                        for (int j = 0; j < countCommand; j++)
                        {
                            if (Business.Market.CommandExecutor[j].Investor.InvestorID == Business.Market.InvestorList[i].InvestorID)
                            {
                                bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Business.Market.CommandExecutor[j].Type.ID);
                                if (!isPending)
                                {
                                    double tempProfit = Math.Round(Business.Market.CommandExecutor[j].Profit, 2);
                                    Business.Market.CommandExecutor[j].Profit = tempProfit;
                                    listOpenTrade.Add(Business.Market.CommandExecutor[j]);
                                }
                                else
                                {
                                    double tempProfit = Math.Round(Business.Market.CommandExecutor[j].Profit, 2);
                                    Business.Market.CommandExecutor[j].Profit = tempProfit;

                                    listPendingOrder.Add(Business.Market.CommandExecutor[j]);
                                }
                            }
                        }
                    }
                    #endregion

                    #region GET COMMAND HISTORY OF INVESTOR
                    listCommandHistory = TradingServer.Facade.FacadeGetCommandHistoryWithTime(Business.Market.InvestorList[i].InvestorID, timeStartDay, timeEndDay);
                    #endregion

                    Model.MailConfig newMailConfig = new Model.MailConfig();
                    newMailConfig = this.GetMailConfig(Business.Market.InvestorList[i]);

                    #region SEND REPORT IF COMMAND > 0
                    if (listOpenTrade != null && listOpenTrade.Count > 0 ||
                       listPendingOrder != null && listPendingOrder.Count > 0 ||
                       listCommandHistory != null && listCommandHistory.Count > 0)
                    {
                        StringBuilder content = new StringBuilder();
                        Business.StatementTemplate newTemplate = new StatementTemplate();

                        content = newTemplate.GetStatementReport(listOpenTrade, listCommandHistory, listPendingOrder, Business.Market.InvestorList[i], timeEndDay);

                        content.Replace("[ReportDay]", timeStartDay.ToShortDateString());

                        string tempContent = content.ToString();

                        #region CHECK SCALPER INVESTOR
                        bool isScalper = this.CheckScalperInvestor(listCommandHistory);
                        #endregion

                        if (Business.Market.InvestorList[i].SendReport)
                        {
                            if (newMailConfig.isEnable)
                            {
                                if (!string.IsNullOrEmpty(newMailConfig.SmtpHost) && !string.IsNullOrEmpty(newMailConfig.MessageFrom) &&
                                        !string.IsNullOrEmpty(newMailConfig.PasswordCredential))
                                {
                                    if (!string.IsNullOrEmpty(Business.Market.InvestorList[i].Email))
                                    {
                                        content = content.Replace("[ScalperName]", "");
                                        content = content.Replace("[Scalper]", "");

                                        Model.TradingCalculate.Instance.SendMailMonthAsync(Business.Market.InvestorList[i].Email, "Monthly Statement", content.ToString(), newMailConfig);
                                    }
                                }
                            }
                        }

                        if (!string.IsNullOrEmpty(pathStatement) && pathStatement != "NaN")
                        {
                            //STREAM FILE SAVE CONTENT SEND MAIL
                            TradingServer.Model.TradingCalculate.Instance.StreamMonthFile(content.ToString(), Business.Market.InvestorList[i].Code, timeStartDay, 0, pathStatement, isScalper);
                        }

                        if (!string.IsNullOrEmpty(content.ToString()))
                        {
                            Business.Statement newStatement = new Statement();
                            newStatement.Content = content.ToString();
                            newStatement.Email = Business.Market.InvestorList[i].Email;
                            newStatement.InvestorCode = Business.Market.InvestorList[i].Code;
                            newStatement.StatementType = 2;
                            newStatement.TimeStatement = timeStartDay;

                            Business.Market.ListStatement.Add(newStatement);
                        }
                    }
                    #endregion
                }
            }
            #endregion      
     
            if (!string.IsNullOrEmpty(pathStatement) && pathStatement != "NaN")
            {
                TradingServer.Model.TradingCalculate.Instance.StreamMonthFile(Business.Market.LogContentSendMailMonth, "", timeStartDay, 1, pathStatement);
                string folder = timeStartDay.Year.ToString() + timeStartDay.Month.ToString();
                TradingServer.Model.TradingCalculate.Instance.ZipFolder(pathStatement + @"\" + folder, folder + ".zip", pathStatement + @"\" + folder + ".zip");
            }
        }
        /// <summary>
        /// SEND REPORT IN DAY
        /// </summary>
        /// <param name="TargetName"></param>
        internal void SendReportDay(string TargetName, Business.TimeEvent timeEvent)
        {
            return;

            string total = string.Empty;
            Business.Market.LogContentSendMail = string.Empty;

            #region GET END OF DAY
            DateTime timeCurrent = DateTime.Now;
            DateTime timeEndDay = new DateTime();
            DateTime timeStartDay = new DateTime();
            bool IsReportWeekends = false;
            string pathStatements = string.Empty;
            string serverName = string.Empty;

            #region GET MARKET CONFIG
            if (Business.Market.MarketConfig != null)
            {
                int count = Business.Market.MarketConfig.Count;
                for (int i = 0; i < count; i++)
                {
                    if (Business.Market.MarketConfig[i].Code == "C02")
                    {
                        serverName = Business.Market.MarketConfig[i].StringValue;
                    }

                    if (Business.Market.MarketConfig[i].Code == "C26")
                    {
                        if (Business.Market.MarketConfig[i].BoolValue == 1)
                            IsReportWeekends = true;
                    }

                    if (Business.Market.MarketConfig[i].Code == "C35")
                    {
                        pathStatements = Business.Market.MarketConfig[i].StringValue;
                    }
                }
            }
            #endregion   
         
            if (string.IsNullOrEmpty(pathStatements))
                pathStatements = Environment.CurrentDirectory + DateTime.Now.Ticks + "_" + serverName;

            if (DateTime.Now.Hour < 10)
                timeCurrent = timeCurrent.AddDays(-1);

            timeStartDay = new DateTime(timeCurrent.Year, timeCurrent.Month, timeCurrent.Day , 00, 00, 00);
            timeEndDay = new DateTime(timeCurrent.Year, timeCurrent.Month, timeCurrent.Day, 23, 59, 59);            
            #endregion

            #region CHECK Generate statements at weekends
            if (timeStartDay.DayOfWeek == DayOfWeek.Saturday || timeStartDay.DayOfWeek == DayOfWeek.Sunday)
            {
                if (!IsReportWeekends)
                    return;
            }
            #endregion
                        
            //CLEAR LIST END OF DAY AGENT
            Business.Market.ListEODAgent.Clear();

            #region SEND MAIL TO INVESTOR
            if (Business.Market.InvestorList != null && Business.Market.InvestorList.Count > 0)
            {   
                int count = Business.Market.InvestorList.Count;
                for (int i = 0; i < count; i++)
                {
                    List<Business.OpenTrade> listOpenTrade = new List<OpenTrade>();
                    List<Business.OpenTrade> listPendingOrder = new List<OpenTrade>();
                    List<Business.OpenTrade> listCommandHistory = new List<OpenTrade>();

                    #region GET ONLINE COMMAND OF INVESTOR
                    if (Business.Market.InvestorList[i].CommandList != null)
                    {
                        int countCommand = Business.Market.InvestorList[i].CommandList.Count;
                        for (int j = 0; j < countCommand; j++)
                        {
                            bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Business.Market.InvestorList[i].CommandList[j].Type.ID);
                            if (!isPending)
                            {
                                double tempProfit = Math.Round(Business.Market.InvestorList[i].CommandList[j].Profit, 2);
                                Business.Market.InvestorList[i].CommandList[j].Profit = tempProfit;
                                listOpenTrade.Add(Business.Market.InvestorList[i].CommandList[j]);
                            }
                            else
                            {
                                double tempProfit = Math.Round(Business.Market.InvestorList[i].CommandList[j].Profit, 2);
                                Business.Market.InvestorList[i].CommandList[j].Profit = tempProfit;
                                listPendingOrder.Add(Business.Market.InvestorList[i].CommandList[j]);
                            }
                        }
                    }
                    #endregion

                    #region GET COMMAND HISTORY OF INVESTOR
                    listCommandHistory = TradingServer.Facade.FacadeGetCommandHistoryWithTime(Business.Market.InvestorList[i].InvestorID, timeStartDay, timeEndDay);
                    #endregion

                    #region DATA > 0
                    if (listOpenTrade != null && listOpenTrade.Count > 0 ||
                      listPendingOrder != null && listPendingOrder.Count > 0 ||
                      listCommandHistory != null && listCommandHistory.Count > 0)
                    {
                        Model.MailConfig newMailConfig = new Model.MailConfig();
                        newMailConfig = this.GetMailConfig(Business.Market.InvestorList[i]);

                        double tempBalance = Business.Market.InvestorList[i].PreviousLedgerBalance;

                        StringBuilder content = new StringBuilder();
                        Business.ReportItem contentOpenPosition = new ReportItem();
                        Business.ReportItem contentClosePosition = new ReportItem();
                        StringBuilder contentPendingPosition = new StringBuilder();

                        Business.StatementTemplate newTemplate = new StatementTemplate();

                        content = newTemplate.GetStatementReport(listOpenTrade, listCommandHistory, listPendingOrder, Business.Market.InvestorList[i], timeEndDay);
                        content.Replace("[ReportDay]", timeEndDay.ToShortDateString());

                        string tempContent = content.ToString();

                        #region CHECK SCALPER INVESTOR
                        bool isScalper = this.CheckScalperInvestor(listCommandHistory);
                        #endregion

                        if (Business.Market.InvestorList[i].SendReport)
                        {
                            #region GET MAIL CONFIG AND SEND REPORT
                            if (newMailConfig != null)
                            {
                                if (newMailConfig.isEnable)
                                {
                                    if (!string.IsNullOrEmpty(newMailConfig.SmtpHost) && !string.IsNullOrEmpty(newMailConfig.MessageFrom) &&
                                        !string.IsNullOrEmpty(newMailConfig.PasswordCredential))
                                    {
                                        if (!string.IsNullOrEmpty(Business.Market.InvestorList[i].Email))
                                        {
                                            content = content.Replace("[ScalperName]", "");
                                            content = content.Replace("[Scalper]", "");

                                            if (timeStartDay.DayOfWeek == DayOfWeek.Sunday || timeStartDay.DayOfWeek == DayOfWeek.Saturday)
                                            {
                                                if (IsReportWeekends)
                                                {
                                                    Model.TradingCalculate.Instance.SendMailAsync(Business.Market.InvestorList[i].Email, "Daily Confirmation", content.ToString(), newMailConfig);
                                                }
                                            }
                                            else
                                            {
                                                Model.TradingCalculate.Instance.SendMailAsync(Business.Market.InvestorList[i].Email, "Daily Confirmation", content.ToString(), newMailConfig);
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion
                        }

                        if (!string.IsNullOrEmpty(pathStatements) && pathStatements != "NaN")
                        {   
                            //STREAM FILE SAVE CONTENT SEND MAIL REPORT
                            TradingServer.Model.TradingCalculate.Instance.StreamFile(tempContent.ToString(), Business.Market.InvestorList[i].Code, timeStartDay, 0, pathStatements, isScalper);
                        }

                        if (!string.IsNullOrEmpty(content.ToString()))
                        {
                            Business.Statement newStatement = new Statement();
                            newStatement.InvestorCode = Business.Market.InvestorList[i].Code;
                            newStatement.StatementType = 1;
                            newStatement.TimeStatement = timeStartDay;
                            newStatement.Email = Business.Market.InvestorList[i].Email;
                            newStatement.Content = tempContent.ToString();

                            Business.Market.ListStatement.Add(newStatement);
                        }
                    }
                    #endregion

                    #region UPDATE PREVIOUS LEDGER BALANCE
                    TradingServer.Facade.FacadeUpdatePreviousLedgerBalance(Business.Market.InvestorList[i].InvestorID, Business.Market.InvestorList[i].PreviousLedgerBalance);
                    #endregion  
             
                    #region PROCESS LAST ACCOUNT
                    Business.SumLastAccount newSumLastAccount = new SumLastAccount();
                    newSumLastAccount.InvestorAccount = Business.Market.InvestorList[i];
                    newSumLastAccount.ListHistory = listCommandHistory;

                    if (newSumLastAccount.ListOpenTrade == null)
                        newSumLastAccount.ListOpenTrade = new List<OpenTrade>();
                     
                    if (listOpenTrade != null)
                    {
                        int countOpenTrade = listOpenTrade.Count;
                        for (int j = 0; j < countOpenTrade; j++)
                        {
                            Business.OpenTrade newOpenTrade = new OpenTrade();
                            newOpenTrade.AgentCommission = listOpenTrade[j].AgentCommission;
                            newOpenTrade.ClientCode = listOpenTrade[j].ClientCode;
                            newOpenTrade.ClosePrice = listOpenTrade[j].ClosePrice;
                            newOpenTrade.CloseTime = listOpenTrade[j].CloseTime;
                            newOpenTrade.CommandCode = listOpenTrade[j].CommandCode;
                            newOpenTrade.Comment = listOpenTrade[j].Comment;
                            newOpenTrade.Commission = listOpenTrade[j].Commission;
                            newOpenTrade.ExpTime = listOpenTrade[j].ExpTime;
                            newOpenTrade.FreezeMargin = listOpenTrade[j].FreezeMargin;
                            newOpenTrade.ID = listOpenTrade[j].ID;
                            newOpenTrade.IGroupSecurity = listOpenTrade[j].IGroupSecurity;
                            newOpenTrade.Investor = listOpenTrade[j].Investor;
                            newOpenTrade.IsClose = listOpenTrade[j].IsClose;
                            newOpenTrade.IsHedged = listOpenTrade[j].IsHedged;
                            newOpenTrade.Margin = listOpenTrade[j].Margin;
                            newOpenTrade.MaxDev = listOpenTrade[j].MaxDev;
                            newOpenTrade.NumberUpdate = listOpenTrade[j].NumberUpdate;
                            newOpenTrade.OpenPrice = listOpenTrade[j].OpenPrice;
                            newOpenTrade.OpenTime = listOpenTrade[j].OpenTime;

                            double tempProfit = listOpenTrade[j].Profit;
                            newOpenTrade.Profit = tempProfit;

                            newOpenTrade.Size = listOpenTrade[j].Size;
                            newOpenTrade.SpreaDifferenceInOpenTrade = listOpenTrade[j].SpreaDifferenceInOpenTrade;
                            newOpenTrade.StopLoss = listOpenTrade[j].StopLoss;
                            newOpenTrade.Swap = listOpenTrade[j].Swap;
                            newOpenTrade.Symbol = listOpenTrade[j].Symbol;
                            newOpenTrade.TakeProfit = listOpenTrade[j].TakeProfit;
                            newOpenTrade.Taxes = listOpenTrade[j].Taxes;
                            newOpenTrade.TotalSwap = listOpenTrade[j].TotalSwap;
                            newOpenTrade.Type = listOpenTrade[j].Type;

                            newSumLastAccount.ListOpenTrade.Add(newOpenTrade);
                        }
                    }
                    
                    newSumLastAccount.TimeEndDay = timeEndDay;

                    Business.Market.ListLastAccount.Add(newSumLastAccount);
                    #endregion
                }
            }
            #endregion

            #region SAVE FILE TO HARD DRIVE
            if (!string.IsNullOrEmpty(pathStatements) && pathStatements != "NaN")
            {
                TradingServer.Model.TradingCalculate.Instance.StreamFile(Business.Market.LogContentSendMail, "", timeStartDay, 1, pathStatements);
                string folder = timeStartDay.Year.ToString() + timeStartDay.Month.ToString() + timeStartDay.Day.ToString();
                TradingServer.Model.TradingCalculate.Instance.ZipFolder(pathStatements + @"\" + folder, folder + ".zip", pathStatements + @"\" + folder + ".zip");
            }
            #endregion
        }