Example #1
0
        public CApplicationMessage UpdateAndSendReport(string ClientKey, string inputValue)
        {
            inputValue = CXML.HtmlDecode(inputValue);
            // B1. Update Ngay thang nhan xuong db
            // B2. Lay file Pdf report
            // B3. Gui Email
            // B4. Update trang thai gui email
            //

            /*
             * <InputValue UserID="1" />
             * <RequestParams Action="" ReceiveResponseDateTime="" ObjectID="" ObjectEmail=""  WarranterEmail="" />
             * <RequestParams Context="" RegisTransRefNo="" ViewID="" ViewerID="" RefNo="" ObjectID=""/>
             * */
            // Update ngay thang xuong db
            string localInputValue = inputValue;

            localInputValue = CXML.Query(inputValue, "InputValue,RequestParams[1]");
            string actionResult = new Core.CoreService().ExecuteAction(ClientKey, localInputValue);
            string Action       = CXML.GetXmlNodeValue(localInputValue, "RequestParams/@Action");
            bool   isSendCSGT   = false;
            string booleanStr   = CXML.GetXmlNodeValue(localInputValue, "RequestParams/@IsSendCSGT");

            bool.TryParse(booleanStr, out isSendCSGT);
            booleanStr = CXML.GetXmlNodeValue(localInputValue, "RequestParams/@IsSendKH");
            bool isSendKH = false;

            bool.TryParse(booleanStr, out isSendKH);

            // Lay file pdf

            string reportInputValue = CXML.Query(inputValue, "InputValue,RequestParams[2]");

            byte[] reportContent   = null;// GetReportPdf(reportInputValue);
            string reportContext   = CXML.GetXmlNodeValue(reportInputValue, "RequestParams/@Context");
            string regisTransRefNo = CXML.GetXmlNodeValue(reportInputValue, "RequestParams/@RegisTransRefNo");
            string viewerID        = CXML.GetXmlNodeValue(reportInputValue, "RequestParams/@ViewerID");
            //string booleanStr= CXML.GetXmlNodeValue(reportInputValue, "RequestParams/@IsSendCSGT");

            // gui email
            //localInputValue = CXML.Query(inputValue, "InputValue,RequestParams[1]");

            string CAEmail = "", KHEmail = "";
            CApplicationMessage sendResult = null;
            int countSuccessCA = 0, countFailCA = 0, countSuccessKH = 0, countFailKH = 0;

            if (Action.ToUpper() == "UPDATE_REQUEST")
            {
                // Truong hop in rieng thi gui moi don mot email
                // - Goi ham Init de tao ra TBGT: Function= Transaction_TBGT_CreateData
                // - Goi ham lay list TBGT '<InputValue UserID="14" /><RequestParams RefType="22" Level="0" RegisTransRefNo="1153900217"/>'
                // - Xuat

                // Goi ham tao Report
                reportInputValue = CXML.AddXmlAttribute(reportInputValue, "RequestParams", "Function", "Transaction_TBGT_CreateData");
                actionResult     = new FWS.VnAccounting.Service.Data.Core.Class.CCoreService().ExecuteAction(ClientKey, reportInputValue);
                CApplicationMessage mess = CDataParser.FromCSV <CApplicationMessage>(actionResult, 0)[0];

                if (!mess.IsSuccessfull)
                {
                    return(mess);
                }

                //Goi ham lay danh sach report can gui email
                int level = 0;
                if (reportContext.ToUpper() == "COMBINE")
                {
                    level = 1;
                }
                string getReportlistInput = CXML.Query(inputValue, "InputValue");
                getReportlistInput += string.Format("<RequestParams Function=\"GetTransaction_Asset\" RefType=\"22\" Level=\"{0}\" RegisTransRefNo=\"{1}\"/>", level, regisTransRefNo);
                actionResult        = new FWS.VnAccounting.Service.Data.Core.Class.CCoreService().GetContextData(ClientKey, getReportlistInput);
                List <CTransactions> transactions = CDataParser.FromCSV <CTransactions>(actionResult, 1);
                if (transactions == null || transactions.Count == 0)
                {
                    return(new CApplicationMessage()
                    {
                        Description = "Không có dữ liệu để gửi"
                    });
                }
                for (int i = 0; i < transactions.Count; i++)
                {
                    //Xuat bao cao pdf
                    reportInputValue  = CXML.Query(inputValue, "InputValue");
                    reportInputValue += string.Format("<RequestParams ID=\"{0}\" Context=\"{4}\" RefNo=\"{1}\" RegisTransRefNo=\"{2}\" ViewerID=\"{3}\" IncludeSign=\"1\"/>", transactions[i].ID, transactions[i].RefNo, transactions[i].RegisTransRefNo, viewerID, reportContext);
                    reportContent     = GetReportPdf(reportInputValue);


                    //duyet qua tung bao cao de xuat bao cao va chay du lieu
                    CAEmail = CXML.GetXmlNodeValue(localInputValue, "RequestParams/@ObjectEmail");
                    if (!string.IsNullOrEmpty(CAEmail) && isSendCSGT)
                    {
                        string sendmainInput = string.Format("<RequestParams Email=\"{0}\" Context=\"{1}\" />", CAEmail, "CONGAN");
                        sendResult = SendReportViaEmail(sendmainInput, reportContent, "", null);

                        // neu gui mail cho CA thanh cong thi moi gui toi KH
                        if (sendResult.IsSuccessfull)
                        {
                            countSuccessCA++;
                        }
                        else
                        {
                            countFailCA++;
                        }
                    }
                    //Bo chuc nang gui email den KH
                    KHEmail = CXML.GetXmlNodeValue(localInputValue, "RequestParams/@WarranterEmail");
                    if (!string.IsNullOrEmpty(KHEmail) && isSendKH)
                    {
                        string sendmainInput = string.Format("<RequestParams Email=\"{0}\" Context=\"{1}\" />", KHEmail, "KHACHHANG");
                        sendResult = SendReportViaEmail(sendmainInput, reportContent, "", null);
                        if (sendResult.IsSuccessfull)
                        {
                            countSuccessKH++;
                        }
                        else
                        {
                            countFailKH++;
                        }
                    }
                    //Neu gui thanh cong va co gui den CSGT thi cap nhat lai email
                    if (isSendCSGT && countSuccessCA > 0)
                    {
                        getReportlistInput  = CXML.Query(inputValue, "InputValue");
                        getReportlistInput += string.Format("<RequestParams Function=\"Transaction_Asset\" Action=\"UPDATE_REQUEST\" RefType=\"22\" ID=\"{0}\" RefNo=\"{1}\" RegisTransRefNo=\"{2}\"/>", transactions[i].ID, transactions[i].RefNo, transactions[i].RegisTransRefNo);
                        actionResult        = new FWS.VnAccounting.Service.Data.Core.Class.CCoreService().ExecuteAction(ClientKey, getReportlistInput);
                        // gui email thanh cong thi update lai thoi gian gui mail
                    }
                }
            }
            else if (Action.ToUpper() == "UPDATE_RESPONSE")
            {
                reportInputValue = CXML.AddXmlAttribute(reportInputValue, "RequestParams", "IncludeSign", "1");
                reportContent    = GetReportPdf(reportInputValue);

                KHEmail = CXML.GetXmlNodeValue(localInputValue, "RequestParams/@WarranterEmail");
                string ResponseStatus = CXML.GetXmlNodeValue(localInputValue, "RequestParams/@ResponseStatus");
                if (ResponseStatus == "0")
                {
                    localInputValue = string.Format("<RequestParams Email=\"{0}\" Context=\"{1}\" />", KHEmail, "PHANHOI_CAPNHAT");
                    sendResult      = SendReportViaEmail(localInputValue, reportContent, "", null);
                    if (sendResult.IsSuccessfull)
                    {
                        countSuccessKH++;
                    }
                    else
                    {
                        countFailKH++;
                    }
                }
                else if (ResponseStatus == "1")
                {
                    localInputValue = string.Format("<RequestParams Email=\"{0}\" Context=\"{1}\" />", KHEmail, "PHANHOI_TUCHOI");
                    sendResult      = SendReportViaEmail(localInputValue, reportContent, "", null);
                    if (sendResult.IsSuccessfull)
                    {
                        countSuccessKH++;
                    }
                    else
                    {
                        countFailKH++;
                    }
                }
            }
            else if (Action == "UPDATE_SENDTHONGBAOPHI")
            {
                reportInputValue = CXML.AddXmlAttribute(reportInputValue, "RequestParams", "IncludeSign", "1");
                reportContent    = GetReportPdf(reportInputValue);

                KHEmail = CXML.GetXmlNodeValue(localInputValue, "RequestParams/@Email");

                localInputValue = string.Format("<RequestParams Email=\"{0}\" Context=\"{1}\" />", KHEmail, "THONGBAO_PHI");
                sendResult      = SendReportViaEmail(localInputValue, reportContent, "", null);
                if (sendResult.IsSuccessfull)
                {
                    countSuccessKH++;
                }
                else
                {
                    countFailKH++;
                }
            }

            string message = "";

            if (countSuccessCA > 0)
            {
                message += string.Format("Gửi thành công {0} email tới {1}\n", countSuccessCA, CAEmail);
            }
            if (countFailCA > 0)
            {
                message += string.Format("{0} email tới {1} bị lỗi\n", countFailCA, CAEmail);
            }
            if (countSuccessKH > 0)
            {
                message += string.Format("Gửi thành công {0} email tới {1}\n", countSuccessKH, KHEmail);
            }
            if (countFailKH > 0)
            {
                message += string.Format("{0} email tới {1} bị lỗi\n", countFailKH, KHEmail);
            }
            if (message.EndsWith("\n"))
            {
                message = message.Remove(message.Length - 1);
            }
            // update ket qua gui email xuong db
            //localInputValue = localInputValue = CXML.Query(inputValue, "InputValue");
            //if (sendResult.IsSuccessfull)
            //{

            //}
            //new Core.CoreService().ExecuteAction(ClientKey, localInputValue);

            //return string.Format("Code;Name;Description;Result\n{0};{1};{2};{3}","OK",message,message,1);
            return(new CApplicationMessage()
            {
                Result = 1, Name = message, Description = message, Code = "OK"
            });

            //return CApplicationMessage.ToCSV<CApplicationMessage>(
            //return new CApplicationMessage() { Result = 1, Name = "OK", Description = "Success", Code = "OK" };
        }