public string SOA_Misc_Credit ( string source_chanel , string in_card_nbr,string in_amount, string in_desc, string in_SS )
 {
     string result = "";
     TTTSOA.TrungTamThe ttt = new TTTSOA.TrungTamThe ( );
     result = ttt.SOA_Misc_Credit ( source_chanel, in_card_nbr, in_amount, in_desc, in_SS );
     return result;
 }
    private void Authorize_FundTrans()
    {
        try
        {
            string sql = "", err = "";
            string //CARD_NUMBER = "",
            ACCOUNT_NUMBER = "";
            //card_nbr = (string)Session["Card_nbr"];
            string batch = ""; string ref_no_br = ""; string txn_amount = ""; string user_duyet = "";

            sql = "select * from nab_balance_prepaid_realtime a where a.ref_no='" + lb_ref_no_Fund_Tran.Text + "' and a.authorize='Y'";
            DataTable dtcheck = executedb.getTable(sql, ref err);

            if (err != "")
            {
                executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), sql + " --> " + err);
                Alert.Show("Error when executing from database!!!", this);
            }
            else
            {
                if (dtcheck.Rows.Count > 0)
                {
                    executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), "Da duyet Record nay: ref_no: " + lb_ref_no_Fund_Tran.Text);
                    Alert.Show("Da duyet record nay!!!", this);
                    return;
                }
            }
            //CARD_NUMBER = lbCardNbr.Text.Trim() == "" ? "null" : ("'" + lbCardNbr.Text.Trim() + "'");
            ACCOUNT_NUMBER = tbTAcc.Text.Trim ( ) == "" ? "null" : ( "" + tbTAcc.Text.Trim ( ) + "" );

            //1. goi ham hach toan
            //    if ok 
            //        (
            //          ghi logfile
            //          update electra
            //        )

            //#region HachToan
            string branchcode = "", branch = "", txnbranch = "", txnacc = "", txnccy = "", txnamount = "", offsetbrn = "", offsetacc = ""
                , offsetccy = "", offsetamount = "", narrative = "", product = "";
            //Nộp tiền mặt
            if (Session["UserBranch"].ToString().Trim() == "")
            {
                executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), "Section bị ngắt. Bạn vui lòng đăng nhập lại");
                //executedb.WriteLogPri(Environment.NewLine + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + " - " + Session["Username"].ToString() + ": " + sql + " --> " + err);
                //Response.Write("<script>alert('Error when executing from database!!!')</script>");
                Alert.Show("Section bị ngắt. Bạn vui lòng đăng nhập lại", this);
                return;
            }
            else
            {
                branchcode = Session["UserBranch"].ToString().Trim();
                branch = Session["UserBranch"].ToString().Trim();
                // offsetbrn = Session["UserBranch"].ToString().Trim().Substring(1, 3);//dung tai chi nhanh nao thi lay tai khoan tien mat tai chi nhanh do
            }
            txnbranch = tbFAcc.Text.Trim ( ).Substring ( 0, 3 );
            txnacc = tbFAcc.Text.Trim();
            txnccy = "VND";
            txnamount = tbBalanceInsert_Fund_Tran.Text.Trim().Replace(",", "").Replace(".", "");
            offsetbrn = tbTAcc_Branch.Text.Trim();//.Substring(1,3);
            if (tbTAcc.Text.Trim().Substring(0, 1) == "5")
                offsetacc = executedb.InternalPrepaid;//"427302002";//tbTAcc.Text.Trim();
            else
                offsetacc = executedb.DomesticPrepaid;// "427301002";//tbTAcc.Text.Trim();

            //datpt 24/05/2013: change 4273 to 4599
            offsetacc = executedb.TK_TamTreoNapTien;
            offsetbrn = executedb.Br_TamTreoNapTien;
            //end datpt 24/05/2013: change 4273 to 4599


            offsetccy = "VND";
            offsetamount = tbBalanceInsert_Fund_Tran.Text.Trim().Replace(",", "").Replace(".", "");
            narrative = tbDecription_Fund_Tran.Text;
            product = executedb.sp_chuyenkhoan;// "GL03";//TEST chuyen khoan
            //, , offsetamount, narrative, product
            //Gọi hàm hạch toán
            string receive = CoreAuthorize.HachToanCheo(branchcode, branch, txnbranch, txnacc, txnccy, txnamount, offsetbrn, offsetacc, offsetccy, offsetamount, narrative, product);
            //string receive = "success|Note lam sau";
            if (receive.Trim() != "")
            {
                if (receive.Substring(0, 7).ToUpper() == "success".ToUpper())
                {
                    batch = tbBatchID_fundsTran.Text.Trim ( );
                    ref_no_br = lb_ref_no.Text;
                    txn_amount = txnamount;
                    user_duyet = Session["Username"].ToString();
                    #region new
                    string result = "";
                    try
                    {
                        TTTSOA.TrungTamThe fundstrans = new TTTSOA.TrungTamThe ( ); ;
                        result = fundstrans.SOA_Misc_Credit ( "WEB", ACCOUNT_NUMBER.Trim(), txnamount, "CHUYEN KHOAN VAO TAI KHOAN", "82" );
                        DataTable check = Splitxml.getxmlvalue ( result );
                        if (check.Rows[0]["result"].ToString().Trim() == "0")
                        {
                            sql  = " update nab_balance_prepaid_realtime b ";
                            sql += "   set b.authorize = 'Y' ";
                            sql += "       ,b.extract_flag='Y' ";
                            sql += "       ,b.user_extract= '" + user_duyet + "'";
                            sql += "       ,b.date_extract= sysdate";
                            sql += "       ,b.message_receive= '" + receive + "'";
                            sql += "       , b.user_modify= '" + user_duyet + "'";
                            sql += "       ,b.DATE_MODIFY= sysdate";
                            sql += "       ,b.microfilm_ref_number = '" + check.Rows[0]["SRN"].ToString().Trim() + "'";
                            sql += "   where b.batch_id = '" + batch + "'";
                            sql += "   and b.ref_no = '" + ref_no_br + "' ";
                            executedb.ExecuteDML ( sql, ref err );
                            if (err != "")
                            {
                                ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                          "err_msg",
                                          "alert('Authorize Fail !!');",
                                          true );
                                executedb.WriteLogERROR ( Session["Username"].ToString ( ), "IP: " + Request.UserHostAddress, this.Page.ToString ( ), System.Reflection.MethodBase.GetCurrentMethod ( ).ToString ( ), " Update not success:" + sql + " Error:" + err );
                            }
                            else
                            {
                                ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                              "err_msg",
                                              "alert('Authorize Success !!');",
                                              true );
                                executedb.WriteLogERROR ( Session["Username"].ToString ( ), "IP: " + Request.UserHostAddress, this.Page.ToString ( ), System.Reflection.MethodBase.GetCurrentMethod ( ).ToString ( ), " Update success:" + sql + " Error:" + err );
                            }
                            
                        }

                        else
                        {
                            executedb.WriteLogERROR ( Session["Username"].ToString ( ), "IP: " + Request.UserHostAddress, this.Page.ToString ( ), System.Reflection.MethodBase.GetCurrentMethod ( ).ToString ( ), "Authorize not success: " + check.Rows[2]["msg"].ToString().Trim() );
                            ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                             "err_msg",
                                             "alert('Mics not success!!');",
                                             true );
                        }
                    }
                    catch (Exception ex)
                    {
                        executedb.WriteLogERROR ( Session["Username"].ToString ( ), "IP: " + Request.UserHostAddress, this.Page.ToString ( ), System.Reflection.MethodBase.GetCurrentMethod ( ).ToString ( ), "Problem in calling SOA --> SOA_Mics_Credit : " + ex.ToString ( ) );
                        ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                              "err_msg",
                                              "alert('Error in calling SOA --> SOA_Mics_Credit ');",
                                              true );
                    }

                    #endregion new
                    
                }//Hach toan khong thanh cong
                else
                {
                    executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), "Authorize not success: " + receive);
                    ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                            "err_msg",
                                            "alert('Failed to call from core!!');",
                                            true );
                }
            }
            else
            {
                executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), "Khong nhan duoc receive tu CORE");
                ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                       "err_msg",
                                       "alert('Khong nhan duoc receive tu CORE');",
                                       true );
            }
        }

        catch (Exception ex)
        {
            #region logException
            executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), ex.Message);
            //executedb.WriteLogPri(Environment.NewLine + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + " - " + Session["Username"].ToString() + ": " + ex.Message);
            //Response.Write("<script>alert('Error when executing coding')</script>");
            ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                      "err_msg",
                                      "alert('Error when executing coding');",
                                      true );
            #endregion logException
        }
    }
    private void Authorize_NopTienMat()
    {
        try
        {
            string sql = "", err = "";
            string ACCOUNT_NUMBER = "",
            account_nbr = (string)Session["Account_nbr"];
            //card_nbr = (string)Session["Card_nbr"];
            
            string batch = ""; string ref_no_br = ""; string txn_amount = ""; string user_duyet = "";

            sql = "select * from nab_balance_prepaid_realtime a where a.ref_no='" + lb_ref_no.Text + "' and a.authorize='Y'";
            DataTable dtcheck = executedb.getTable(sql, ref err);

            if (err != "")
            {
                executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), sql + " --> " + err);
                Alert.Show("Error when executing from database!!!", this);
            }
            else
            {
                if (dtcheck.Rows.Count > 0)
                {
                    executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), "Da duyet Record nay: ref_no: " + lb_ref_no.Text);
                    Alert.Show("Da duyet record nay!!!", this);
                    return;
                }
            }

            // batch = ""; ref_no_br = lb_ref_no.Text; txn_amount = ""; user_duyet = "";
            ACCOUNT_NUMBER = tbAccountnbr.Text.Trim ( ) == "" ? "null" : ( "" + tbAccountnbr.Text.Trim() + "" );


            //1. goi ham hach toan
            //    if ok 
            //        (
            //          ghi logfile
            //          update electra
            //        )

            string branchcode = "", branch = "", txnbranch = "", txnacc = "", txnccy = "", txnamount = "", offsetbrn = "", offsetacc = ""
                , offsetccy = "", offsetamount = "", narrative = "", product = "";
            //Nộp tiền mặt
            if (Session["UserBranch"].ToString().Trim() == "")
            {
                executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), "Section bị ngắt. Bạn vui lòng đăng nhập lại");
                //executedb.WriteLogPri(Environment.NewLine + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + " - " + Session["Username"].ToString() + ": " + sql + " --> " + err);
                //Response.Write("<script>alert('Error when executing from database!!!')</script>");
                Alert.Show("Section bị ngắt. Bạn vui lòng đăng nhập lại", this);

                return;
            }
            else
            {
                branchcode = Session["UserBranch"].ToString().Trim();
                branch = Session["UserBranch"].ToString().Trim();
                offsetbrn = Session["UserBranch"].ToString().Trim();//dung tai chi nhanh nao thi lay tai khoan tien mat tai chi nhanh do
            }
            //Lấy branch tài khoản thẻ:
            sql = ""; err = "";
            sql = "select ctr.contract_number card_number, b.code branch_code";
            sql += "  from " + executedb.owner + "acnt_contract ctr, " + executedb.owner + "branch b";
            sql += " where ctr.amnd_state = 'A'";
            sql += "   and ctr.con_cat = 'A' ";
            sql += "   and b.amnd_state = 'A' ";
            sql += "   and ctr.branch = b.code ";
            sql += "   and ctr.contract_number = '" + account_nbr.Trim ( ) + "'";

            DataTable dtcheckbranchcard = executedb.getTable(sql, ref err);

            if (err != "")
            {
                executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), sql + " --> " + err);
                Alert.Show("Error when executing from database!!!", this);
            }
            else
            {
                if (dtcheckbranchcard.Rows.Count <= 0)
                {
                    executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), "Khong the tim thay so the nay:" + lbCardNbr.Text.Trim());
                    Alert.Show("Khong the tim thay so the nay:" + lbCardNbr.Text.Trim(), this);
                    return;
                }
                else
                    if (dtcheckbranchcard.Rows.Count >= 2)
                    {
                        executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), "Nhieu hon 1 branch duoc tim thay:" + lbCardNbr.Text.Trim());
                        Alert.Show("Nhieu hon 1 branch duoc tim thay:" + lbCardNbr.Text.Trim(), this);
                        return;
                    }
                    else
                    {
                        txnbranch = dtcheckbranchcard.Rows[0]["branch_code"].ToString().Trim();
                    }
            }
            if (tbAccountnbr.Text.Trim ( ).Substring ( 0, 1 ) == "5")
                txnacc = executedb.InternalPrepaid;//"427302002";//fix for prepaid card
            else
                txnacc = executedb.DomesticPrepaid;// "427301002";

            //datpt 24/05/2013: change 4273 to 4599
            txnacc = executedb.TK_TamTreoNapTien;
            txnbranch = executedb.Br_TamTreoNapTien;
            //end datpt 24/05/2013: change 4273 to 4599


            txnccy = "VND";
            txnamount = tbBalanceInsert.Text.Trim().Replace(",", "").Replace(".", "");
            offsetacc = executedb.TKTienmat;// "101101001";
            offsetccy = "VND";
            offsetamount = tbBalanceInsert.Text.Trim().Replace(",", "").Replace(".", "");
            narrative = "BALANCE PREPAID CARD";
            product = executedb.sp_noptienmat;// "NT04";//TEST nap tien mat
            //, , offsetamount, narrative, product
            //Gọi hàm hạch toán
            string receive = CoreAuthorize.HachToanCheo(branchcode, branch, txnbranch, txnacc, txnccy, txnamount, offsetbrn, offsetacc, offsetccy, offsetamount, narrative, product);
            //string receive= "success|Note lam sau";
            
            if (receive.Trim() != "")
            //if(true)
            {
                if (receive.Substring(0, 7).ToUpper() == "success".ToUpper())
                {
                    batch = tbBatchID1.Text.Trim ( );
                    ref_no_br = lb_ref_no.Text;
                    txn_amount = txnamount;
                    user_duyet = Session["Username"].ToString();

                    //Write log success
                    executedb.WriteLogINFO ( Session["Username"].ToString ( ), "IP: " + Request.UserHostAddress, this.Page.ToString ( ), System.Reflection.MethodBase.GetCurrentMethod ( ).ToString ( ), "Authorize result: " + receive);
                    //Goi procedure a Phuong viet de xu ly cap nhat balance electra

                    //const int para = 2;
                    try
                    {
                        string usermodify = "";//cardnbr = "", 
                        usermodify = (string)Session["Username"];
                        if (ref_no_br != "")
                        {
                            #region new
                            string result = "";
                            try
                            {
                                TTTSOA.TrungTamThe noptienmat = new TTTSOA.TrungTamThe ( ); ;
                                result = noptienmat.SOA_Misc_Credit ( "WEB", ACCOUNT_NUMBER.Trim(), txn_amount, "NOP TIEN VAO TAI KHOAN", "82" );
                                DataTable check = Splitxml.getxmlvalue ( result );
                                if (check.Rows[0]["result"].ToString ( ).Trim ( ) == "0")
                                {
                                    sql = " update nab_balance_prepaid_realtime b ";
                                    sql += "   set b.authorize = 'Y' ";
                                    sql += "       ,b.extract_flag='Y' ";
                                    sql += "       ,b.user_extract= '" + user_duyet + "'";
                                    sql += "       ,b.date_extract= sysdate";
                                    sql += "       ,b.message_receive= '" + receive + "'";
                                    sql += "       , b.user_modify= '" + user_duyet + "'";
                                    sql += "       ,b.DATE_MODIFY= sysdate";
                                    sql += "       ,b.microfilm_ref_number = '" + check.Rows[0]["SRN"].ToString ( ).Trim ( ) + "'";
                                    sql += "   where b.batch_id = '" + batch + "'";
                                    sql += "   and b.ref_no = '" + ref_no_br + "' ";

                                    executedb.ExecuteDML ( sql, ref err );
                                    
                                    if (err !="")
                                    {               
                                        ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                            "err_msg",
                                            "alert('Authorize Fail !!');",
                                            true );
                                        
                                    }
                                    else                       
                                    {
                                        ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                              "err_msg",
                                              "alert('Authorize Success !!');",
                                              true );
                                        
                                    }
                                    executedb.WriteLogERROR ( Session["Username"].ToString ( ), "IP: " + Request.UserHostAddress, this.Page.ToString ( ), System.Reflection.MethodBase.GetCurrentMethod ( ).ToString ( ), "Authorize success: " + check.Rows[0]["msg"].ToString ( ) + " Update success:" +sql+ " Error:" + err );
                                }
                                else
                                {
                                    executedb.WriteLogERROR ( Session["Username"].ToString ( ), "IP: " + Request.UserHostAddress, this.Page.ToString ( ), System.Reflection.MethodBase.GetCurrentMethod ( ).ToString ( ), "Authorize not success: " + check.Rows[0]["msg"].ToString ( ) + " " + ACCOUNT_NUMBER.ToString().Trim() );
                                    ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                              "err_msg",
                                              "alert('Mics not success --> SOA_Mics_Credit');",
                                              true );
                                }
                            #endregion new
                            }
                            catch     (Exception ex)
                            {
                                executedb.WriteLogERROR ( Session["Username"].ToString ( ), "IP: " + Request.UserHostAddress, this.Page.ToString ( ), System.Reflection.MethodBase.GetCurrentMethod ( ).ToString ( ), "Problem in calling SOA --> SOA_Misc_Credit --> " + ex.ToString ( ) );
                                ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                             "err_msg",
                                             "alert('Error in calling SOA --> SOA_Mics_Credit');",
                                             true );
                            }
                        }

                        tbBalanceInsert.ReadOnly = true;
                        LoadGrid();
                    }
                    catch (Exception ex)
                    {
                        #region logException
                        executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), ex.Message);
                        ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                             "err_msg",
                                             "alert('Error when executing coding!!!');window.location='../Default.aspx';",
                                             true );
                        #endregion logException
                    }
                }//Hach toan khong thanh cong
                else
                {
                    executedb.WriteLogERROR ( Session["Username"].ToString ( ), "IP: " + Request.UserHostAddress, this.Page.ToString ( ), System.Reflection.MethodBase.GetCurrentMethod ( ).ToString ( ), "Authorize not success: " + receive);
                    ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                            "err_msg",
                                            "alert('Check FCC port, authorize not success!!');",
                                            true );
                }
            }
            else
            {
                executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), "Khong nhan duoc receive tu CORE");
                ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                       "err_msg",
                                       "alert('Khong nhan duoc receive tu CORE');",
                                       true );
            }

        }
        catch (Exception ex)
        {
            #region logException
            executedb.WriteLogERROR(Session["Username"].ToString(), "IP: " + Request.UserHostAddress, this.Page.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), ex.Message);
            ScriptManager.RegisterStartupScript ( this.Page, this.Page.GetType ( ),
                                            "err_msg",
                                            "alert('Error when executing coding!!!');window.location='../Default.aspx';",
                                            true );
            #endregion logException
        }
    }