using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DSWeb.Models;
using DSWeb.EntityDA;
using System.Text;
using System.Collections.Generic;
using System.Xml.Linq;
using JsonHelper;
using System.Xml;

namespace DSWeb.CW
{
    public partial class CwVouchersAddDoGridSource : System.Web.UI.Page
    {
        private string strReadXmlType = "";//读取xml串方式 "init"-初始化获取所有账户信息;"add"-添加新的账户信息;"delete"-删除账户信息;"exist"查看是否有与委托相关账户
        private int iShowCount;//每页显示数据量

        //delete
        private string strCwVouchersAddDoGid;//账户信息GID
        private string strHandle;//账户操作说明
        private const int iResult = -99;//参数错误

        T_ALL_DA T_ALL_DA = new T_ALL_DA();
        private string strRange = String.Empty;//权限可视范围
        RangeDA RangeDA = new RangeDA();
        public string strUserID;//用户GID
        public string strORDNO;//
        public string strGIDS;//

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USERID"] != null)
            {
                strUserID = Session["USERID"].ToString().Trim();
            }
            //
            #region 判断参数是否正确
            if (Request.QueryString["read"] != null)
            {
                strReadXmlType = Request.QueryString["read"].ToString().Trim();
            }
            if (Request.QueryString["showcount"] != null)
            {
                iShowCount = int.Parse(Request.QueryString["showcount"].ToString());
            }
            if (Request.QueryString["ordno"] != null)
            {
                strORDNO = Request.QueryString["ordno"].ToString();
            }
            if (Request.QueryString["gids"] != null)
            {
                strGIDS = Request.QueryString["gids"].ToString();
            }
            #endregion

            /*
            //权限可视范围
            strRange = RangeDA.GetVISIBLERANGE(strUserID.Trim(), "modOrderManagement");
            if (strRange.Trim().Equals("0"))//全部
            {
                strRange = "";
            }
            else if (strRange.Trim().Equals("1"))//分公司
            {
                strRange = " and BSNO in(" + RangeDA.GetOrderManagementCompany(strUserID.Trim()) + ")";
            }
            else if (strRange.Trim().Equals("2"))//部门
            {
                strRange = " and BSNO in(" + RangeDA.GetOrderManagementDEPT(strUserID.Trim()) + ")";
            }
            else if (strRange.Trim().Equals("3"))//个人
            {
                strRange = " and BSNO in(" + RangeDA.GetOrderManagementPerson(strUserID.Trim()) + ")";
            }
            else if (strRange.Trim().Equals("4"))//无
            {
                strRange = " and 1<0";
            }
            else//空
            {
                strRange = " and 1<0";
            }
            */

            //
            if (!strReadXmlType.Equals(""))
            {
                if (strReadXmlType.Equals("delete") || strReadXmlType.Equals("recover"))
                {
                    strCwVouchersAddDoGid = (String)Request.QueryString["gid"] as String;
                    strHandle = (String)Request.QueryString["read"] as String;
                    if (strCwVouchersAddDoGid == null || strHandle == null)
                    {
                        Response.Write(iResult);
                    }
                    else
                    {
                        string strQuery = DoExcute(strCwVouchersAddDoGid, strHandle);
                        Response.Write(strQuery);
                    }
                }
                else if (strReadXmlType.Equals("isvouchers"))
                {
                    string strQuery = setVouchers();
                    Response.Write(strQuery);
                }
                else
                {
                    string strOutputXml = "";
                    strOutputXml = GetCells(iShowCount, strReadXmlType);
                    //输出XML字符串
                    Response.ContentType = "text/xml";
                    strOutputXml.Replace("&", "&amp;");
                    Response.Write(strOutputXml);
                }
            }
            else
            {
                //访问参数不正确
                Response.ContentType = "text/xml";
                Response.Write("-2");
            }

        }

        /// <summary>
        /// 根据用户提交的
        /// </summary>
        /// <param name="tempGid">账户GID</param>
        /// <param name="tempHandle">操作类型 delete删除单条账户</param>
        /// <returns>值1-删除成功;值-1 -删除操作异常 值-2 -回滚失败 值-3 -未获取相关账户信息</returns>
        private string DoExcute(string tempGid, string tempHandle)
        {
            string strVal = "";
            CwVouitemsDA CwVouitemsDA = new CwVouitemsDA();

            //操作类型为删除
            if (tempHandle == "delete")
            {
                int iResult = 0;
                //先判断当前要删除的账户是否存在
                if (!tempGid.Trim().Equals(""))
                {
                    CwVouitemsEntity CwVouitemsEntity = new CwVouitemsEntity();
                    CwVouitemsEntity = CwVouitemsDA.GetModel(tempGid);

                    if (CwVouitemsEntity.GID != null)
                    {
                        iResult = CwVouitemsDA.Delete(CwVouitemsEntity.GID);
                    }
                    else
                    {
                        iResult = -3; //未获取相关账户信息
                    }
                }
                strVal = iResult.ToString();
            }

            if (tempHandle == "recover")
            {
                if (!tempGid.Trim().Equals(""))
                {
                    CwVouitemsEntity CwVouitemsEntity = new CwVouitemsEntity();
                    CwVouitemsEntity = CwVouitemsDA.GetModel(tempGid);

                    if (CwVouitemsEntity != null)
                    {
                        StringBuilder CwVouitemsBuilder = new StringBuilder();
                        CwVouitemsBuilder.Append(CwVouitemsEntity.GID + ",");
                        CwVouitemsBuilder.Append(CwVouitemsEntity.ITEMNO + ",");
                        CwVouitemsBuilder.Append(CwVouitemsEntity.EXPLAN + ",");
                        CwVouitemsBuilder.Append(CwVouitemsEntity.ACCID + ",");
                        CwVouitemsBuilder.Append(CwVouitemsEntity.ACCNAME + ",");
                        CwVouitemsBuilder.Append(CwVouitemsEntity.AMTDR + ",");
                        CwVouitemsBuilder.Append(CwVouitemsEntity.AMTCR + ",");
                        CwVouitemsBuilder.Append(CwVouitemsEntity.FCYDR + ",");
                        CwVouitemsBuilder.Append(CwVouitemsEntity.FCYCR + ",");
                        CwVouitemsBuilder.Append(CwVouitemsEntity.FCYEXRATE + ",");
                        CwVouitemsBuilder.Append(CwVouitemsEntity.CUSTOMERNAME + ",");
                        CwVouitemsBuilder.Append(CwVouitemsEntity.CORPID + ",");
                        CwVouitemsBuilder.Append(CwVouitemsEntity.SALE + ",");
                        CwVouitemsBuilder.Append(CwVouitemsEntity.BLNO);
                        strVal = CwVouitemsBuilder.ToString();
                    }
                    else
                    {
                        strVal = "-3";
                    }
                }
                else
                {
                    strVal = "-3";
                }
            }
            return strVal;
        }

        /// <summary>
        /// 获取账户信息
        /// </summary>
        /// <returns></returns>
        private string GetCells(int iShowCount, string readXmlType)
        {
            CwVouitemsDA CwVouchersAddDoEntity = new CwVouitemsDA();
            CwVouitemsDA CwVouitemsDA = new CwVouitemsDA();

            if (!strReadXmlType.Equals("exist"))
            {
                //获取所有账户信息,用做Grid显示
                DataTable CwVouchersAddDoTable = new DataTable();

                //初始化
                string strInitSql = " SELECT GID,ITEMNO,EXPLAN,ACCID,ACCNAME,AMTDR,AMTCR,FCYDR,FCYCR,FCYEXRATE,CUSTOMERNAME,CORPID,SALE,BLNO FROM cw_vouitems WHERE 1=1 and ORDNO='" + strORDNO.ToString().Trim() + "'" + strRange + " ORDER BY ITEMNO";
                CwVouchersAddDoTable = getStatusNameTable(CwVouitemsDA.GetExcuteSql(strInitSql).Tables[0]);

                //编排字符串 xml串
                StringBuilder dataBuilder = new StringBuilder();

                dataBuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                dataBuilder.Append("<rows>");

                int iCount = CwVouchersAddDoTable.Rows.Count;

                for (int i = 0; i < iCount; i++)
                {
                    int jCount = CwVouchersAddDoTable.Columns.Count;
                    dataBuilder.Append("<row id=\"" + CwVouchersAddDoTable.Rows[i]["GID"].ToString() + "\">");
                    dataBuilder.Append("<cell>0</cell>");
                    for (int j = 1; j < jCount; j++)
                    {
                        switch (j)
                        {
                            case 1:
                                dataBuilder.Append("<cell>" + CwVouchersAddDoTable.Rows[i][j].ToString() + "</cell>");
                                break;
                            case 2:
                                dataBuilder.Append("<cell>" + CwVouchersAddDoTable.Rows[i][j].ToString() + "</cell>");
                                break;
                            case 3:
                                string xl = GetACCID(CwVouchersAddDoTable.Rows[i][j].ToString().Trim());
                                if (xl != "")
                                {
                                    dataBuilder.Append(xl);
                                }
                                else
                                {
                                    dataBuilder.Append("<cell>" + CwVouchersAddDoTable.Rows[i][j].ToString() + "</cell>");
                                }
                                break;
                            case 4:
                                dataBuilder.Append("<cell>" + CwVouchersAddDoTable.Rows[i][j].ToString() + "</cell>");
                                break;
                            case 5:
                                dataBuilder.Append("<cell>" + CwVouchersAddDoTable.Rows[i][j].ToString() + "</cell>");
                                break;
                            case 6:
                                dataBuilder.Append("<cell>" + CwVouchersAddDoTable.Rows[i][j].ToString() + "</cell>");
                                break;
                            case 7:
                                dataBuilder.Append("<cell>" + CwVouchersAddDoTable.Rows[i][j].ToString() + "</cell>");
                                break;
                            case 8:
                                dataBuilder.Append("<cell>" + CwVouchersAddDoTable.Rows[i][j].ToString() + "</cell>");
                                break;
                            case 9:
                                dataBuilder.Append("<cell>" + CwVouchersAddDoTable.Rows[i][j].ToString() + "</cell>");
                                break;
                            case 10:
                                xl = GetCRMClientCells(CwVouchersAddDoTable.Rows[i][j].ToString().Trim());
                                if (xl != "")
                                {
                                    dataBuilder.Append(xl);
                                }
                                else
                                {
                                    dataBuilder.Append("<cell>" + CwVouchersAddDoTable.Rows[i][j].ToString() + "</cell>");
                                }
                                break;
                            case 11:
                                dataBuilder.Append("<cell>" + CwVouchersAddDoTable.Rows[i][j].ToString() + "</cell>");
                                break;
                            case 12:
                                xl = GetSALE(CwVouchersAddDoTable.Rows[i][j].ToString().Trim());
                                if (xl != "")
                                {
                                    dataBuilder.Append(xl);
                                }
                                else
                                {
                                    dataBuilder.Append("<cell>" + CwVouchersAddDoTable.Rows[i][j].ToString() + "</cell>");
                                }
                                break;
                            case 13:
                                dataBuilder.Append("<cell>" + CwVouchersAddDoTable.Rows[i][j].ToString() + "</cell>");
                                break;
                        }

                    }
                    dataBuilder.Append("</row>");
                }
                dataBuilder.Append("</rows>");

                return dataBuilder.ToString();
            }
            else if (strReadXmlType.Equals("exist"))
            {
                return "1";//存在账户
            }
            else if (strReadXmlType.Equals("add"))
            {
                StringBuilder dataBuilder = new StringBuilder();
                dataBuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                dataBuilder.Append("<rows>");
                dataBuilder.Append("<row id=\"" + Guid.NewGuid().ToString() + "\">");
                //
                dataBuilder.Append("<cell>0</cell>");
                dataBuilder.Append("<cell>" + GetITEMNO() + "</cell>");
                dataBuilder.Append("<cell></cell>");
                //
                string strcb = GetACCID("");
                if (strcb.Trim() == "")
                {
                    dataBuilder.Append("<cell></cell>");
                }
                else
                {
                    dataBuilder.Append(strcb);
                }
                //
                dataBuilder.Append("<cell></cell>");
                dataBuilder.Append("<cell>0</cell>");
                dataBuilder.Append("<cell>0</cell>");
                dataBuilder.Append("<cell>0</cell>");
                dataBuilder.Append("<cell>0</cell>");
                dataBuilder.Append("<cell>0</cell>");
                //
                strcb = GetCRMClientCells("");
                if (strcb.Trim() == "")
                {
                    dataBuilder.Append("<cell></cell>");
                }
                else
                {
                    dataBuilder.Append(strcb);
                }
                //
                dataBuilder.Append("<cell></cell>");
                //
                strcb = GetSALE("");
                if (strcb.Trim() == "")
                {
                    dataBuilder.Append("<cell></cell>");
                }
                else
                {
                    dataBuilder.Append(strcb);
                }
                //
                dataBuilder.Append("<cell></cell>");
                //
                dataBuilder.Append("</row>");
                dataBuilder.Append("</rows>");
                return dataBuilder.ToString();
            }
            else
            {
                return "-3";//没有相关的账户
            }
        }

        /// <summary>
        /// 获取数据库顺序递增的行号
        /// </summary>
        /// <returns></returns>
        private string GetITEMNO()
        {
            CwVouitemsDA CwVouitemsDA = new CwVouitemsDA();
            string strITEMNO = CwVouitemsDA.GetStrSQL("hh", "select isnull(max(ITEMNO),0)+1 as hh from Cw_Vouitems where ORDNO='" + strORDNO + "'");
            return strITEMNO;
        }

        /// <summary>
        /// 获取科目信息下拉列表
        /// </summary>
        /// <param name="strClientValue">当前列表科目信息id值</param>
        /// <returns></returns>
        public string GetACCID(string strACCID)
        {
            T_ALL_DA T_ALL_DA = new T_ALL_DA();
            DataSet ds = T_ALL_DA.GetAllSQL("select * from cw_accitems order by ACCID");
            if (ds != null)
            {
                StringBuilder clientBuilder = new StringBuilder();
                clientBuilder.Append("<cell xmlcontent=\"1\" editable=\"1\">" + strACCID);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (!ds.Tables[0].Rows[i]["ACCID"].ToString().Trim().Equals(""))
                    {
                        clientBuilder.Append("<option value=\"" + ds.Tables[0].Rows[i]["ACCID"].ToString() + "\">" + ds.Tables[0].Rows[i]["ACCID"].ToString() + " | " + ds.Tables[0].Rows[i]["ACCNAME"].ToString() + "</option>");
                    }
                }
                clientBuilder.Append("</cell>");
                return clientBuilder.ToString();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 获取CRM系统客户信息下拉列表
        /// </summary>
        /// <param name="strClientValue">当前列表客户信息值</param>
        /// <returns></returns>
        public string GetCRMClientCells(string strClientValue)
        {
            CRMClientDA crmClientDA = new CRMClientDA();
            DataTable clientTable = crmClientDA.GetCRMClientList().Tables[0];
            if (clientTable != null)
            {
                StringBuilder clientBuilder = new StringBuilder();
                clientBuilder.Append("<cell xmlcontent=\"1\" editable=\"1\">" + strClientValue);
                for (int i = 0; i < clientTable.Rows.Count; i++)
                {
                    if (!clientTable.Rows[i]["SHORTNAME"].ToString().Trim().Equals(""))
                    {
                        string ls = clientTable.Rows[i]["CODENAME"].ToString().ToUpper() + " | " + clientTable.Rows[i]["SHORTNAME"].ToString();
                        clientBuilder.Append("<option value=\"" + ls + "\">" + ls + "</option>");
                    }
                }
                clientBuilder.Append("</cell>");
                return clientBuilder.ToString();
            }
            else
            {
                return "";
            }
        }


        /// <summary>
        /// 获取揽货人信息下拉列表
        /// </summary>
        /// <param name="strClientValue">当前列表揽货人信息值</param>
        /// <returns></returns>
        public string GetSALE(string strClientValue)
        {
            T_ALL_DA T_ALL_DA = new T_ALL_DA();
            DataSet ds = T_ALL_DA.GetOP(strUserID, "isSaleMan");
            if (ds != null)
            {
                StringBuilder SALEBuilder = new StringBuilder();
                SALEBuilder.Append("<cell xmlcontent=\"1\" editable=\"1\">" + strClientValue);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (!ds.Tables[0].Rows[i]["CODENAME"].ToString().Trim().Equals(""))
                    {
                        string ls = ds.Tables[0].Rows[i]["CODENAME"].ToString().ToUpper() + " | " + ds.Tables[0].Rows[i]["SHOWNAME"].ToString();
                        SALEBuilder.Append("<option value=\"" + ls + "\">" + ls + "</option>");
                    }
                }
                SALEBuilder.Append("</cell>");
                return SALEBuilder.ToString();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 将数据集表中账户状态CwVouchersAddDoStatus和应收应付类型CwVouchersAddDoType的数字状态位转换成文字
        /// </summary>
        /// <param name="tempTable">原数据源DataTable</param>
        /// <returns>返回新数据源DataTable</returns>
        private DataTable getStatusNameTable(DataTable tempTable)
        {
            DataTable sourceTable = tempTable;
            DataTable cloneTable = new DataTable();
            int iSwitch = 0;
            for (int i = 0; i < sourceTable.Rows.Count; i++)
            {
                if (iSwitch == 0)
                {
                    for (int j = 0; j < sourceTable.Columns.Count; j++)
                    {
                        DataColumn newColumn = new DataColumn();
                        newColumn.ColumnName = sourceTable.Columns[j].ColumnName;
                        newColumn.DataType = sourceTable.Columns[j].DataType;
                        cloneTable.Columns.Add(newColumn);
                    }
                    iSwitch = 1;
                }
                DataRow cloneRow = cloneTable.NewRow();

                for (int k = 0; k < sourceTable.Columns.Count; k++)
                {
                    cloneRow[sourceTable.Columns[k].ColumnName] = sourceTable.Rows[i][k];
                }
                cloneTable.Rows.Add(cloneRow);
            }
            return cloneTable;
        }

        
        protected String getgid()
        {
            string strgid = Guid.NewGuid().ToString();
            strgid = strgid.Replace("-", "");
            strgid = "VOU" + strgid;
            return strgid;
        }

        protected String setVouchers()
        {
            string sSQL = "";
            string sResult = "";
            string zt = "";
            //
            if (strGIDS.Trim() != "")
            {
                //帐套设置
                sSQL = "SELECT top 1 * from cw_design";
                DataSet ds = T_ALL_DA.GetAllSQL(sSQL);
                if (ds == null)
                {
                    return "请先设置帐套信息!";
                }
                //
                string sGids = strGIDS.Trim().Replace(",", "','");
                sGids = "'" + sGids.Trim() + "'";
                //合并生成结算凭证
                if (ds.Tables[0].Rows[0]["ISMGSC"].ToString().Trim() == "True")
                {
                    sResult = getSC(ds, sGids.Trim(), "");//, strEXPLAN, ds5.Tables[0].Rows[0]["DEPTID"].ToString().Trim(), ds5.Tables[0].Rows[0]["EMPLID"].ToString().Trim(), ds5.Tables[0].Rows[0]["SALE"].ToString().Trim());
                    zt += sResult;
                }
                else
                {
                    string[] items = sGids.Trim().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    if (items.Length > 0)
                    {
                        for (int i = 0; i < items.Length; i++)
                        {
                            sResult = getSC(ds, items[i].ToString().Trim(), "");//, strEXPLAN, ds5.Tables[0].Rows[0]["DEPTID"].ToString().Trim(), ds5.Tables[0].Rows[0]["EMPLID"].ToString().Trim(), ds5.Tables[0].Rows[0]["SALE"].ToString().Trim());
                            zt += "," + sResult;
                        }
                        if (zt.Trim().Length > 0)
                        {
                            zt = zt.Trim().Substring(1);
                        }
                    }
                }
                //
                if (zt.IndexOf("-") > 0)
                {
                     return "";
                }
            }
            return zt;
        }

        /// <summary>
        /// 生成凭证
        /// </summary>
        /// <param name="ds">帐套设置数据集</param>
        /// <param name="sGids">gid业务编号</param>
        /// <param name="strVESSEL">船名航次</param>
        /// <param name="strEXPLAN">规则生成的摘要</param>
        /// <returns></returns>
        protected String getSC(DataSet ds, string sGids, string strVESSEL)//, string strEXPLAN, string strDEPTID, string strEMPLID, string strSALE)
        {
            //全部
            string sSQL = "";
            string strEXPLAN = "";
            Decimal iAMTDR = 0;
            Decimal iAMTCR = 0;
            int iResult = 0;
            //差额主营业务收入
            Decimal dRMBS = 0;
            Decimal dRMBF = 0;
            Decimal dUSDS = 0;
            Decimal dUSDF = 0;
            int n = 0;
            Decimal dEXCHANGERATE = 0;

            //其他常用科目设置 应收RMB
            sSQL = "SELECT top 1 * from cw_accitems where ACCID='" + ds.Tables[0].Rows[0]["AR"].ToString().Trim() + "'";
            DataSet ds2 = T_ALL_DA.GetAllSQL(sSQL);
            //其他常用科目设置 应付RMB
            sSQL = "SELECT top 1 * from cw_accitems where ACCID='" + ds.Tables[0].Rows[0]["AP"].ToString().Trim() + "'";
            DataSet ds3 = T_ALL_DA.GetAllSQL(sSQL);
            //其他常用科目设置 应收USD
            sSQL = "SELECT top 1 * from cw_accitems where ACCID='" + ds.Tables[0].Rows[0]["ARFC"].ToString().Trim() + "'";
            DataSet ds6 = T_ALL_DA.GetAllSQL(sSQL);
            //其他常用科目设置 应付USD
            sSQL = "SELECT top 1 * from cw_accitems where ACCID='" + ds.Tables[0].Rows[0]["APFC"].ToString().Trim() + "'";
            DataSet ds7 = T_ALL_DA.GetAllSQL(sSQL);
            //其他常用科目设置 付费结算借方默认科目代码
            sSQL = "SELECT top 1 * from cw_accitems where ACCID='" + ds.Tables[0].Rows[0]["APMBSPENDING"].ToString().Trim() + "'";
            DataSet ds8 = T_ALL_DA.GetAllSQL(sSQL);
            //其他常用科目设置 付费结算外币借方默认科目代码
            sSQL = "SELECT top 1 * from cw_accitems where ACCID='" + ds.Tables[0].Rows[0]["APTMSFC"].ToString().Trim() + "'";
            DataSet ds9 = T_ALL_DA.GetAllSQL(sSQL);
            //
            //ISRPCLIENTNAME	是否收付客户名称
            //ISRPCOTCODE	是否收付业务编号
            //ISRPETD	是否收付航期

            //ISRPMBLNO	是否收付主提单号
            //ISRPCUSTNO	是否收付委托编号
            //ISRPVESSEL	是否收付船名
            //ISRPVOYNO	是否收付航次

            string strISEXPLAN = "";
            //if (ds.Tables[0].Rows[0]["ISRPCLIENTNAME"].ToString().Trim() == "True")
            //{
            //    strISEXPLAN += " " + ds.Tables[0].Rows[0]["CUSTOMERNAME"].ToString().Trim();
            //}
            if (ds.Tables[0].Rows[0]["ISRPCUSTNO"].ToString().Trim() == "True")
            {
                strISEXPLAN += ",CUSTNO";
            }
            if (ds.Tables[0].Rows[0]["ISRPMBLNO"].ToString().Trim() == "True")
            {
                strISEXPLAN += ",MBLNO";
            }
            if (ds.Tables[0].Rows[0]["ISRPVESSEL"].ToString().Trim() == "True")
            {
                strISEXPLAN += ",VESSEL";
            }
            if (ds.Tables[0].Rows[0]["ISRPVOYNO"].ToString().Trim() == "True")
            {
                strISEXPLAN += ",VOYNO";
            }
            //
            string strORDNO = getgid();
            CwVouchersDA CwVouchersDA = new CwVouchersDA();
            IList<CwVouitemsEntity> CwVouitemsEntities = new List<CwVouitemsEntity>();
            //
            //(CASE WHEN FEETYPE = 1 THEN '应收账款' ELSE '应付账款' END) as 类型
            //,CUSTOMERNAME 客户名称
            //,EXCHANGERATE 汇率
            //,CURRENCY 币别`
            //,ISADVANCEDPAY 是否垫付
            //,SUM(AMOUNT) AS 金额
            //,SUM(AMOUNT*EXCHANGERATE) AS 合计金额
            sSQL = "SELECT (CASE WHEN FEETYPE = 1 THEN '应收账款' WHEN FEETYPE=2 THEN '应付账款' ELSE '' END) as FEETYPE";
            sSQL += ",FAPCODE=(select top 1 FAPCODE from info_client where SHORTNAME=VW_ch_fee_settlement_do.CUSTOMERNAME)";
            sSQL += ",FARCODE=(select top 1 FARCODE from info_client where SHORTNAME=VW_ch_fee_settlement_do.CUSTOMERNAME)";
            sSQL += ",SETTLETYPE,CUSTOMERNAME,EXCHANGERATE=isnull(EXCHANGERATE,1),CURRENCY,SUM(AMOUNT) AS je,SUM(AMOUNT*isnull(EXCHANGERATE,1)) AS hj"; 
            sSQL += ",ACCOUNTRMB,ACCOUNTUSD" + strISEXPLAN;
            sSQL += " From VW_ch_fee_settlement_do WHERE 1=1 ";
            sSQL += " and gid_s in (" + sGids.Trim() + ") " + strVESSEL;
            sSQL += " GROUP BY FEETYPE,CUSTOMERNAME,EXCHANGERATE,CURRENCY,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD" + strISEXPLAN;//BILLNO,
            sSQL += " ORDER BY FEETYPE desc,CURRENCY,CUSTOMERNAME" + strISEXPLAN;//BILLNO,
            DataSet ds1 = T_ALL_DA.GetAllSQL(sSQL);
            if (ds1 != null)
            {
                string strEXPLANALL = ""; 
                string strBILLNOALL = "";
                //插入明细表
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    n = i;// +1;
                    CwVouitemsEntity CwVouitemsEntity = new CwVouitemsEntity();
                    //CwVouitemsEntity.GID	唯一编码 newid()
                    CwVouitemsEntity.ORDNO = strORDNO.Trim();
                    CwVouitemsEntity.ITEMNO = n;//	行号
                    CwVouitemsEntity.PACCID = "";//父级科目贷码
                    CwVouitemsEntity.PROPERTY = 0;//科目属性
                    CwVouitemsEntity.GRADE = 0;//科目级别
                    if (ds1.Tables[0].Rows[i]["FEETYPE"].ToString().Trim() == "应收账款")
                    {
                        iAMTDR = iAMTDR + Decimal.Parse(ds1.Tables[0].Rows[i]["hj"].ToString().Trim());
                        //
                        if (ds1.Tables[0].Rows[i]["CURRENCY"].ToString().Trim() == "RMB")
                        {
                            CwVouitemsEntity.ACCID = ds.Tables[0].Rows[0]["AR"].ToString().Trim();//应收账款科目贷码
                            CwVouitemsEntity.AMTDR = 0;//本位币贷方金额(互斥)
                            CwVouitemsEntity.AMTCR = Decimal.Parse(ds1.Tables[0].Rows[i]["je"].ToString().Trim());//本位币借方金额(互斥)
                            CwVouitemsEntity.ISFCY = false;//是否外币
                            CwVouitemsEntity.FCYNO = "RMB";//外币编号usd
                            CwVouitemsEntity.FCYDR = 0;//外币借方金额
                            CwVouitemsEntity.FCYCR = 0;//外币贷方金额
                            dRMBS += Decimal.Parse(ds1.Tables[0].Rows[i]["je"].ToString().Trim());//本位币借方金额(互斥)
                            if (ds2 == null)
                            {
                                CwVouitemsEntity.ACCNAME = "";//科目名称
                                CwVouitemsEntity.ISDEPTACC = false;//核算部门,按规则设置生成
                                CwVouitemsEntity.ISEMPLACC = false;//核算人员,按规则设置生成
                                CwVouitemsEntity.ISCORPACC = false;//核算客户,按规则设置生成
                                CwVouitemsEntity.ISITEMACC = false;//核算项目,按规则设置生成
                                CwVouitemsEntity.DC = "D";//贷方向
                            }
                            else
                            {
                                CwVouitemsEntity.ACCNAME = ds2.Tables[0].Rows[0]["ACCNAME"].ToString().Trim();//科目名称
                                CwVouitemsEntity.ISDEPTACC = Boolean.Parse(ds2.Tables[0].Rows[0]["ISDEPTACC"].ToString().Trim());//核算部门,按规则设置生成
                                CwVouitemsEntity.ISEMPLACC = Boolean.Parse(ds2.Tables[0].Rows[0]["ISEMPLACC"].ToString().Trim());//核算人员,按规则设置生成
                                CwVouitemsEntity.ISCORPACC = Boolean.Parse(ds2.Tables[0].Rows[0]["ISCORPACC"].ToString().Trim());//核算客户,按规则设置生成
                                CwVouitemsEntity.ISITEMACC = Boolean.Parse(ds2.Tables[0].Rows[0]["ISITEMACC"].ToString().Trim());//核算项目,按规则设置生成
                                if (ds2.Tables[0].Rows[0]["DC"].ToString().Trim() == "借")
                                {
                                    CwVouitemsEntity.DC = "D";//借方向
                                }
                                else
                                {
                                    CwVouitemsEntity.DC = "C";//贷方向
                                }
                            }
                        }
                        else if (ds1.Tables[0].Rows[i]["CURRENCY"].ToString().Trim() != "RMB")
                        {
                            CwVouitemsEntity.ACCID = ds.Tables[0].Rows[0]["ARFC"].ToString().Trim();//应收账款外币科目贷码
                            CwVouitemsEntity.AMTDR = 0;//本位币贷方金额(互斥)
                            CwVouitemsEntity.AMTCR = Decimal.Parse(ds1.Tables[0].Rows[i]["hj"].ToString().Trim());//本位币借方金额(互斥)
                            CwVouitemsEntity.ISFCY = true;//是否外币
                            CwVouitemsEntity.FCYNO = "USD";//外币编号usd
                            CwVouitemsEntity.FCYDR = 0;//外币贷方金额
                            CwVouitemsEntity.FCYCR = Decimal.Parse(ds1.Tables[0].Rows[i]["je"].ToString().Trim());//外币借方金额
                            dUSDS += Decimal.Parse(ds1.Tables[0].Rows[i]["je"].ToString().Trim());//本位币借方金额(互斥)
                            if (ds6 == null)
                            {
                                CwVouitemsEntity.ACCNAME = "";//科目名称
                                CwVouitemsEntity.ISDEPTACC = false;//核算部门,按规则设置生成
                                CwVouitemsEntity.ISEMPLACC = false;//核算人员,按规则设置生成
                                CwVouitemsEntity.ISCORPACC = false;//核算客户,按规则设置生成
                                CwVouitemsEntity.ISITEMACC = false;//核算项目,按规则设置生成
                                CwVouitemsEntity.DC = "C";//贷方向
                            }
                            else
                            {
                                CwVouitemsEntity.ACCNAME = ds6.Tables[0].Rows[0]["ACCNAME"].ToString().Trim();//科目名称
                                CwVouitemsEntity.ISDEPTACC = Boolean.Parse(ds6.Tables[0].Rows[0]["ISDEPTACC"].ToString().Trim());//核算部门,按规则设置生成
                                CwVouitemsEntity.ISEMPLACC = Boolean.Parse(ds6.Tables[0].Rows[0]["ISEMPLACC"].ToString().Trim());//核算人员,按规则设置生成
                                CwVouitemsEntity.ISCORPACC = Boolean.Parse(ds6.Tables[0].Rows[0]["ISCORPACC"].ToString().Trim());//核算客户,按规则设置生成
                                CwVouitemsEntity.ISITEMACC = Boolean.Parse(ds6.Tables[0].Rows[0]["ISITEMACC"].ToString().Trim());//核算项目,按规则设置生成
                                if (ds6.Tables[0].Rows[0]["DC"].ToString().Trim() == "借")
                                {
                                    CwVouitemsEntity.DC = "D";//借方向
                                }
                                else
                                {
                                    CwVouitemsEntity.DC = "C";//贷方向
                                }
                            }
                        }
                        CwVouitemsEntity.CORPID = ds1.Tables[0].Rows[i]["FAPCODE"].ToString().Trim();//客户(系统)
                        CwVouitemsEntity.CUSTOMERNAME = ds1.Tables[0].Rows[i]["CUSTOMERNAME"].ToString().Trim();//客户(系统)
                    }
                    else if (ds1.Tables[0].Rows[i]["FEETYPE"].ToString().Trim() == "应付账款")
                    {
                        iAMTCR = iAMTCR + Decimal.Parse(ds1.Tables[0].Rows[i]["hj"].ToString().Trim());
                        //
                        if (ds1.Tables[0].Rows[i]["CURRENCY"].ToString().Trim() == "RMB")
                        {
                            if (ds.Tables[0].Rows[0]["ISAPMBSPENDING"].ToString().Trim() == "True")
                            {
                                CwVouitemsEntity.ACCID = ds.Tables[0].Rows[0]["APMBSPENDING"].ToString().Trim();//应付账款科目贷码
                            }
                            else
                            {
                                CwVouitemsEntity.ACCID = ds.Tables[0].Rows[0]["AP"].ToString().Trim();//应付账款科目贷码
                            }
                            CwVouitemsEntity.AMTDR = Decimal.Parse(ds1.Tables[0].Rows[i]["je"].ToString().Trim());//本位币贷方金额(互斥)
                            CwVouitemsEntity.AMTCR = 0;//本位币借方金额(互斥)
                            CwVouitemsEntity.ISFCY = false;//是否外币
                            CwVouitemsEntity.FCYNO = "RMB";//外币编号usd
                            CwVouitemsEntity.FCYDR = 0;//外币借方金额
                            CwVouitemsEntity.FCYCR = 0;//外币贷方金额
                            dRMBF += Decimal.Parse(ds1.Tables[0].Rows[i]["je"].ToString().Trim());//本位币贷方金额(互斥)
                            if (ds3 == null)
                            {
                                CwVouitemsEntity.ACCNAME = "";//科目名称
                                CwVouitemsEntity.ISDEPTACC = false;//核算部门,按规则设置生成
                                CwVouitemsEntity.ISEMPLACC = false;//核算人员,按规则设置生成
                                CwVouitemsEntity.ISCORPACC = false;//核算客户,按规则设置生成
                                CwVouitemsEntity.ISITEMACC = false;//核算项目,按规则设置生成
                                CwVouitemsEntity.DC = "C";//贷方向
                                CwVouitemsEntity.CORPID = "";//客户(系统)
                                CwVouitemsEntity.CUSTOMERNAME = "";//客户(系统)
                            }
                            else
                            {
                                if (ds.Tables[0].Rows[0]["ISAPMBSPENDING"].ToString().Trim() == "True")
                                {
                                    if (ds8 == null)
                                    {
                                        CwVouitemsEntity.ACCNAME = "";//科目名称
                                    }
                                    else
                                    {
                                        CwVouitemsEntity.ACCNAME = ds8.Tables[0].Rows[0]["ACCNAME"].ToString().Trim();//科目名称
                                    }
                                    CwVouitemsEntity.CORPID = "";//客户(系统)
                                    CwVouitemsEntity.CUSTOMERNAME = "";//客户(系统)
                                    CwVouitemsEntity.ISCORPACC = false;//核算客户,按规则设置生成
                                }
                                else
                                {
                                    CwVouitemsEntity.ACCNAME = ds3.Tables[0].Rows[0]["ACCNAME"].ToString().Trim();//科目名称
                                    CwVouitemsEntity.CORPID = ds1.Tables[0].Rows[i]["FARCODE"].ToString().Trim();//客户(系统)
                                    CwVouitemsEntity.CUSTOMERNAME = ds1.Tables[0].Rows[i]["CUSTOMERNAME"].ToString().Trim();//客户(系统)
                                    CwVouitemsEntity.ISCORPACC = Boolean.Parse(ds3.Tables[0].Rows[0]["ISCORPACC"].ToString().Trim());//核算客户,按规则设置生成
                                }
                                CwVouitemsEntity.ISDEPTACC = Boolean.Parse(ds3.Tables[0].Rows[0]["ISDEPTACC"].ToString().Trim());//核算部门,按规则设置生成
                                CwVouitemsEntity.ISEMPLACC = Boolean.Parse(ds3.Tables[0].Rows[0]["ISEMPLACC"].ToString().Trim());//核算人员,按规则设置生成
                                CwVouitemsEntity.ISITEMACC = Boolean.Parse(ds3.Tables[0].Rows[0]["ISITEMACC"].ToString().Trim());//核算项目,按规则设置生成
                                if (ds3.Tables[0].Rows[0]["DC"].ToString().Trim() == "借")
                                {
                                    CwVouitemsEntity.DC = "D";//借方向
                                }
                                else
                                {
                                    CwVouitemsEntity.DC = "C";//贷方向
                                }
                            }
                        }
                        else if (ds1.Tables[0].Rows[i]["CURRENCY"].ToString().Trim() != "RMB")
                        {
                            if (ds.Tables[0].Rows[0]["ISAPTMSFC"].ToString().Trim() == "True")
                            {
                                CwVouitemsEntity.ACCID = ds.Tables[0].Rows[0]["APTMSFC"].ToString().Trim();//应付账款科目贷码
                            }
                            else
                            {
                                CwVouitemsEntity.ACCID = ds.Tables[0].Rows[0]["APFC"].ToString().Trim();//应付账款外币科目贷码
                            }
                            CwVouitemsEntity.AMTDR = Decimal.Parse(ds1.Tables[0].Rows[i]["hj"].ToString().Trim());//本位币贷方金额(互斥)
                            CwVouitemsEntity.AMTCR = 0;//本位币借方金额(互斥)
                            CwVouitemsEntity.ISFCY = true;//是否外币
                            CwVouitemsEntity.FCYNO = "USD";//外币编号usd
                            CwVouitemsEntity.FCYDR = Decimal.Parse(ds1.Tables[0].Rows[i]["je"].ToString().Trim());//外币贷方金额
                            CwVouitemsEntity.FCYCR = 0;//外币借方金额
                            dUSDF += Decimal.Parse(ds1.Tables[0].Rows[i]["je"].ToString().Trim());//本位币贷方金额(互斥)
                            if (ds7 == null)
                            {
                                CwVouitemsEntity.ACCNAME = "";//科目名称
                                CwVouitemsEntity.ISDEPTACC = false;//核算部门,按规则设置生成
                                CwVouitemsEntity.ISEMPLACC = false;//核算人员,按规则设置生成
                                CwVouitemsEntity.ISCORPACC = false;//核算客户,按规则设置生成
                                CwVouitemsEntity.ISITEMACC = false;//核算项目,按规则设置生成
                                CwVouitemsEntity.DC = "C";//贷方向
                                CwVouitemsEntity.CORPID = "";//客户(系统)
                                CwVouitemsEntity.CUSTOMERNAME = "";//客户(系统)
                            }
                            else
                            {
                                if (ds.Tables[0].Rows[0]["ISAPTMSFC"].ToString().Trim() == "True")
                                {
                                    if (ds9 == null)
                                    {
                                        CwVouitemsEntity.ACCNAME = "";//科目名称
                                    }
                                    else
                                    {
                                        CwVouitemsEntity.ACCNAME = ds9.Tables[0].Rows[0]["ACCNAME"].ToString().Trim();//科目名称
                                    }
                                    CwVouitemsEntity.CORPID = "";//客户(系统)
                                    CwVouitemsEntity.CUSTOMERNAME = "";//客户(系统)
                                    CwVouitemsEntity.ISCORPACC = false;//核算客户,按规则设置生成
                                }
                                else
                                {
                                    CwVouitemsEntity.ACCNAME = ds7.Tables[0].Rows[0]["ACCNAME"].ToString().Trim();//科目名称
                                    CwVouitemsEntity.CORPID = ds1.Tables[0].Rows[i]["FARCODE"].ToString().Trim();//客户(系统)
                                    CwVouitemsEntity.CUSTOMERNAME = ds1.Tables[0].Rows[i]["CUSTOMERNAME"].ToString().Trim();//客户(系统)
                                    CwVouitemsEntity.ISCORPACC = Boolean.Parse(ds7.Tables[0].Rows[0]["ISCORPACC"].ToString().Trim());//核算客户,按规则设置生成
                                }
                                CwVouitemsEntity.ISDEPTACC = Boolean.Parse(ds7.Tables[0].Rows[0]["ISDEPTACC"].ToString().Trim());//核算部门,按规则设置生成
                                CwVouitemsEntity.ISEMPLACC = Boolean.Parse(ds7.Tables[0].Rows[0]["ISEMPLACC"].ToString().Trim());//核算人员,按规则设置生成
                                CwVouitemsEntity.ISITEMACC = Boolean.Parse(ds7.Tables[0].Rows[0]["ISITEMACC"].ToString().Trim());//核算项目,按规则设置生成
                                if (ds7.Tables[0].Rows[0]["DC"].ToString().Trim() == "借")
                                {
                                    CwVouitemsEntity.DC = "D";//借方向
                                }
                                else
                                {
                                    CwVouitemsEntity.DC = "C";//贷方向
                                }
                            }
                        }
                        //CwVouitemsEntity.CORPID = ds1.Tables[0].Rows[i]["FARCODE"].ToString().Trim();//客户(系统)
                        //CwVouitemsEntity.CUSTOMERNAME = ds1.Tables[0].Rows[i]["CUSTOMERNAME"].ToString().Trim();//客户(系统)
                    }
                    CwVouitemsEntity.FCYEXRATE = Decimal.Parse(ds1.Tables[0].Rows[i]["EXCHANGERATE"].ToString().Trim());//汇率
                    dEXCHANGERATE = Decimal.Parse(ds1.Tables[0].Rows[i]["EXCHANGERATE"].ToString().Trim());//汇率
                    CwVouitemsEntity.QTYUNIT = "";//数量包装单位
                    CwVouitemsEntity.QTYDR = 0;//借方数量
                    CwVouitemsEntity.QTYCR = 0;//贷方数量
                    //
                    //ISRPCLIENTNAME	是否收付客户名称
                    //ISRPCOTCODE	是否收付业务编号
                    //ISRPETD	是否收付航期

                    //ISRPMBLNO	是否收付主提单号
                    //ISRPCUSTNO	是否收付委托编号
                    //ISRPVESSEL	是否收付船名
                    //ISRPVOYNO	是否收付航次

                    string strSelEXPLAN = "";
                    //if (ds.Tables[0].Rows[0]["ISRPCLIENTNAME"].ToString().Trim() == "True")
                    //{
                    //    strISEXPLAN += " " + ds.Tables[0].Rows[0]["CUSTOMERNAME"].ToString().Trim();
                    //}
                    if (ds.Tables[0].Rows[0]["ISRPCUSTNO"].ToString().Trim() == "True")
                    {
                        strSelEXPLAN += " and CUSTNO = '" + ds1.Tables[0].Rows[i]["CUSTNO"].ToString().Trim() + "'";
                    }
                    if (ds.Tables[0].Rows[0]["ISRPMBLNO"].ToString().Trim() == "True")
                    {
                        strSelEXPLAN += " and MBLNO = '" + ds1.Tables[0].Rows[i]["MBLNO"].ToString().Trim() + "'";
                    }
                    if (ds.Tables[0].Rows[0]["ISRPVESSEL"].ToString().Trim() == "True")
                    {
                        strSelEXPLAN += " and VESSEL = '" + ds1.Tables[0].Rows[i]["VESSEL"].ToString().Trim() + "'";
                    }
                    if (ds.Tables[0].Rows[0]["ISRPVOYNO"].ToString().Trim() == "True")
                    {
                        strSelEXPLAN += " and VOYNO = '" + ds1.Tables[0].Rows[i]["VOYNO"].ToString().Trim() + "'";
                    }
                    //
                    sSQL = "select top 1 * from VW_ch_fee_settlement_do where gid_s in(" + sGids.Trim() + ") and CUSTOMERNAME='" + ds1.Tables[0].Rows[i]["CUSTOMERNAME"].ToString().Trim() + "'" + strSelEXPLAN;//BILLNO
                    DataSet ds5 = T_ALL_DA.GetAllSQL(sSQL);
                    if (ds5 != null)
                    {
                        //ISCLIENTNAME	是否客户名称
                        //ISCUSTNO	是否委托编号
                        //ISMBLNO	是否主提单号
                        //ISVESSEL	是否船名
                        //ISVOYNO	是否航次

                        //ISCOTCODE	是否业务编号
                        //ISETD	是否航期

                        strEXPLAN = "";
                        if (ds.Tables[0].Rows[0]["ISRPCLIENTNAME"].ToString().Trim() == "True")
                        {
                            strEXPLAN += " " + ds5.Tables[0].Rows[0]["CUSTOMERNAME"].ToString().Trim();
                        }
                        if (ds.Tables[0].Rows[0]["ISRPCUSTNO"].ToString().Trim() == "True")
                        {
                            strEXPLAN += " " + ds5.Tables[0].Rows[0]["CUSTNO"].ToString().Trim();
                        }
                        if (ds.Tables[0].Rows[0]["ISRPMBLNO"].ToString().Trim() == "True")
                        {
                            strEXPLAN += " " + ds5.Tables[0].Rows[0]["MBLNO"].ToString().Trim();
                        }
                        if (ds.Tables[0].Rows[0]["ISRPVESSEL"].ToString().Trim() == "True")
                        {
                            strEXPLAN += " " + ds5.Tables[0].Rows[0]["VESSEL"].ToString().Trim();
                        }
                        if (ds.Tables[0].Rows[0]["ISRPVOYNO"].ToString().Trim() == "True")
                        {
                            strEXPLAN += " " + ds5.Tables[0].Rows[0]["VOYNO"].ToString().Trim();
                        }
                    }
                    //
                    CwVouitemsEntity.EXPLAN = strEXPLAN;//摘要,规则生成
                    strEXPLANALL=strEXPLAN;
                    CwVouitemsEntity.DEPTID = "";//strDEPTID;//部门(系统)
                    CwVouitemsEntity.EMPLID = "";//strEMPLID;//人员(系统)
                    CwVouitemsEntity.SALE = "";//strSALE;//人员名称
                    CwVouitemsEntity.PITEMID = 0;//父级项目编号
                    CwVouitemsEntity.ITEMID = "0";//项目编号
                    CwVouitemsEntity.FIELD1 = "";//预留字段1
                    CwVouitemsEntity.FIELD2 = "";//预留字段2
                    CwVouitemsEntity.FIELD3 = "";//预留字段3
                    CwVouitemsEntity.FIELD4 = "";//预留字段4
                    CwVouitemsEntity.FIELD5 = "";//预留字段5
                    CwVouitemsEntity.FIELD6 = "";//预留字段6
                    CwVouitemsEntity.FIELD7 = "";//预留字段7
                    CwVouitemsEntity.FIELD8 = "";//预留字段8
                    CwVouitemsEntity.FIELD9 = "";//预留字段9
                    CwVouitemsEntity.FIELD10 = "";//预留字段10
                    if (ds.Tables[0].Rows[0]["ISVBM"].ToString().Trim() == "True")
                    {
                        CwVouitemsEntity.ISSYS = false;//是否手动录入
                    }
                    else
                    {
                        CwVouitemsEntity.ISSYS = true;//是否手动录入
                    }
                    CwVouitemsEntity.FSETTLCODE = ds1.Tables[0].Rows[i]["SETTLETYPE"].ToString().Trim();//结算方式
                    CwVouitemsEntity.FSETTLENO = "";//财务辅助编号
                    CwVouitemsEntity.MODIFIEDUSER = strUserID;//最后一次更新操作人
                    CwVouitemsEntity.BLNO = "";//BILLNO
                    if (ds.Tables[0].Rows[0]["ISRPMBLNO"].ToString().Trim() == "True")
                    {
                        CwVouitemsEntity.BLNO = ds1.Tables[0].Rows[i]["mblno"].ToString().Trim();//
                    }
                    strBILLNOALL = CwVouitemsEntity.BLNO.ToString().Trim();//
                    CwVouitemsEntities.Add(CwVouitemsEntity);
                }

                //现金、账户
                Decimal dRMB = dRMBS - dRMBF;
                Decimal dUSD = dUSDS - dUSDF;
                //收入\成本 生成 全额\差额
                Decimal strJie = dRMBS + dUSDS * dEXCHANGERATE;
                Decimal strDai = dRMBF + dUSDF * dEXCHANGERATE;
                //
                string ls = "";
                string strBPVW = "";
                //插入明细表
                //
                //(CASE WHEN FEETYPE = 1 THEN '应收账款' ELSE '应付账款' END) as 类型
                //,CUSTOMERNAME 客户名称
                //,EXCHANGERATE 汇率
                //,CURRENCY 币别`
                //,ISADVANCEDPAY 是否垫付
                //,SUM(AMOUNT) AS 金额
                //,SUM(AMOUNT*EXCHANGERATE) AS 合计金额
                sSQL = "SELECT BILLNO,(CASE WHEN FEETYPE = 1 THEN '应收账款' WHEN FEETYPE=2 THEN '应付账款' ELSE '' END) as FEETYPE";
                //sSQL += ",FAPCODE=(select top 1 FAPCODE from info_client where SHORTNAME=VW_ch_fee_settlement_do.CUSTOMERNAME)";
                //sSQL += ",FARCODE=(select top 1 FARCODE from info_client where SHORTNAME=VW_ch_fee_settlement_do.CUSTOMERNAME)";
                sSQL += ",SETTLETYPE,EXCHANGERATE=isnull(EXCHANGERATE,1),CURRENCY,SUM(AMOUNT) AS je,SUM(AMOUNT*isnull(EXCHANGERATE,1)) AS hj"; //CUSTOMERNAME,
                sSQL += ",ACCOUNTRMB,ACCOUNTUSD";// +strISEXPLAN;
                sSQL += " From VW_ch_fee_settlement_do WHERE 1=1 ";
                sSQL += " and gid_s in (" + sGids.Trim() + ") ";// +strVESSEL;
                sSQL += " GROUP BY BILLNO,FEETYPE,EXCHANGERATE,CURRENCY,SETTLETYPE,ACCOUNTRMB,ACCOUNTUSD";//BILLNO,,CUSTOMERNAME + strISEXPLAN
                sSQL += " ORDER BY BILLNO,FEETYPE desc,CURRENCY";//BILLNO,,CUSTOMERNAME + strISEXPLAN
                DataSet ds10 = T_ALL_DA.GetAllSQL(sSQL);
                if (ds10 != null)
                {
                    for (int j = 0; j < ds10.Tables[0].Rows.Count; j++)
                    {
                        n = n + 1;
                        CwVouitemsEntity CwVouitemsEntity2 = new CwVouitemsEntity();
                        //CwVouitemsEntity2.GID	唯一编码 newid()
                        CwVouitemsEntity2.ORDNO = strORDNO.Trim();
                        CwVouitemsEntity2.ITEMNO = n;//	行号
                        CwVouitemsEntity2.PACCID = "";//父级科目贷码
                        CwVouitemsEntity2.PROPERTY = 0;//科目属性
                        CwVouitemsEntity2.GRADE = 0;//科目级别
                        if (ds10.Tables[0].Rows[j]["FEETYPE"].ToString().Trim() == "应收账款")
                        {
                            //iAMTDR = iAMTDR + Decimal.Parse(ds10.Tables[0].Rows[j]["hj"].ToString().Trim());
                            //
                            if (ds10.Tables[0].Rows[j]["CURRENCY"].ToString().Trim() == "RMB")
                            {
                                ls = "select FINANCESOFTCODE from sys_bank where gid='" + ds10.Tables[0].Rows[j]["ACCOUNTRMB"].ToString().Trim() + "'";// in (select top 1 ACCOUNTRMB from ch_fee_settlement where BILLNO='" + ds10.Tables[0].Rows[j]["BILLNO"].ToString().Trim() + "')";
                                string strACCID = T_ALL_DA.GetStrSQL("FINANCESOFTCODE", ls);
                                if (strACCID.Trim() == "")
                                {
                                    strACCID = "1001";
                                }
                                CwVouitemsEntity2.ACCID = strACCID.Trim();//科目贷码
                                ls = "select BANKNAME from sys_bank where gid='" + ds10.Tables[0].Rows[j]["ACCOUNTRMB"].ToString().Trim() + "'";// in (select top 1 ACCOUNTRMB from ch_fee_settlement where BILLNO='" + ds10.Tables[0].Rows[j]["BILLNO"].ToString().Trim() + "')";
                                string strACCNAME = T_ALL_DA.GetStrSQL("BANKNAME", ls);
                                if (strACCNAME.Trim() == "")
                                {
                                    strACCNAME = "现金";
                                }
                                CwVouitemsEntity2.ACCNAME = strACCNAME.Trim();//科目名称
                                //
                                CwVouitemsEntity2.AMTDR = Decimal.Parse(ds10.Tables[0].Rows[j]["je"].ToString().Trim());//本位币贷方金额(互斥)
                                CwVouitemsEntity2.AMTCR = 0;//本位币借方金额(互斥)
                                strDai = strDai + Decimal.Parse(ds10.Tables[0].Rows[j]["je"].ToString().Trim());
                                CwVouitemsEntity2.ISFCY = false;//是否外币
                                CwVouitemsEntity2.FCYNO = "RMB";//外币编号usd
                                CwVouitemsEntity2.FCYDR = 0;//外币借方金额
                                CwVouitemsEntity2.FCYCR = 0;//外币贷方金额
                                CwVouitemsEntity2.ISDEPTACC = false;//核算部门,按规则设置生成
                                CwVouitemsEntity2.ISEMPLACC = false;//核算人员,按规则设置生成
                                CwVouitemsEntity2.ISCORPACC = false;//核算客户,按规则设置生成
                                CwVouitemsEntity2.ISITEMACC = false;//核算项目,按规则设置生成
                                CwVouitemsEntity2.DC = "D";//贷方向
                            }
                            else if (ds10.Tables[0].Rows[j]["CURRENCY"].ToString().Trim() != "RMB")
                            {
                                ls = "select FINANCESOFTCODE from sys_bank where gid='" + ds10.Tables[0].Rows[j]["ACCOUNTUSD"].ToString().Trim() + "'";// in (select top 1 ACCOUNTUSD from ch_fee_settlement where BILLNO='" + ds10.Tables[0].Rows[j]["BILLNO"].ToString().Trim() + "')";
                                string strACCID = T_ALL_DA.GetStrSQL("FINANCESOFTCODE", ls);
                                if (strACCID.Trim() == "")
                                {
                                    strACCID = "1001";
                                }
                                CwVouitemsEntity2.ACCID = strACCID.Trim();//科目贷码
                                ls = "select BANKNAME from sys_bank where gid='" + ds10.Tables[0].Rows[j]["ACCOUNTUSD"].ToString().Trim() + "'";//  in (select top 1 ACCOUNTUSD from ch_fee_settlement where BILLNO='" + ds10.Tables[0].Rows[j]["BILLNO"].ToString().Trim() + "')";
                                string strACCNAME = T_ALL_DA.GetStrSQL("BANKNAME", ls);
                                if (strACCNAME.Trim() == "")
                                {
                                    strACCNAME = "现金";
                                }
                                CwVouitemsEntity2.ACCNAME = strACCNAME.Trim();//科目名称
                                //
                                CwVouitemsEntity2.AMTDR = Decimal.Parse(ds10.Tables[0].Rows[j]["hj"].ToString().Trim());//本位币贷方金额(互斥)
                                CwVouitemsEntity2.AMTCR = 0;//本位币借方金额(互斥)
                                strDai = strDai + Decimal.Parse(ds10.Tables[0].Rows[j]["hj"].ToString().Trim());
                                CwVouitemsEntity2.ISFCY = true;//是否外币
                                CwVouitemsEntity2.FCYNO = "USD";//外币编号usd
                                CwVouitemsEntity2.FCYDR = Decimal.Parse(ds10.Tables[0].Rows[j]["je"].ToString().Trim());//外币借方金额
                                CwVouitemsEntity2.FCYCR = 0;//外币贷方金额
                                CwVouitemsEntity2.ISDEPTACC = false;//核算部门,按规则设置生成
                                CwVouitemsEntity2.ISEMPLACC = false;//核算人员,按规则设置生成
                                CwVouitemsEntity2.ISCORPACC = false;//核算客户,按规则设置生成
                                CwVouitemsEntity2.ISITEMACC = false;//核算项目,按规则设置生成
                                CwVouitemsEntity2.DC = "D";//借方向
                            }
                            CwVouitemsEntity2.CORPID = "";// ds10.Tables[0].Rows[j]["FAPCODE"].ToString().Trim();//客户(系统)
                            CwVouitemsEntity2.CUSTOMERNAME = "";//ds10.Tables[0].Rows[j]["CUSTOMERNAME"].ToString().Trim();//客户(系统)
                            strBPVW = ds.Tables[0].Rows[0]["BRTW"].ToString().Trim();
                        }
                        else if (ds10.Tables[0].Rows[j]["FEETYPE"].ToString().Trim() == "应付账款")
                        {
                            //iAMTCR = iAMTCR + Decimal.Parse(ds10.Tables[0].Rows[j]["hj"].ToString().Trim());
                            //
                            if (ds10.Tables[0].Rows[j]["CURRENCY"].ToString().Trim() == "RMB")
                            {
                                ls = "select FINANCESOFTCODE from sys_bank where gid='" + ds10.Tables[0].Rows[j]["ACCOUNTRMB"].ToString().Trim() + "'";//  in (select top 1 ACCOUNTRMB from ch_fee_settlement where BILLNO='" + ds10.Tables[0].Rows[j]["BILLNO"].ToString().Trim() + "')";
                                string strACCID = T_ALL_DA.GetStrSQL("FINANCESOFTCODE", ls);
                                if (strACCID.Trim() == "")
                                {
                                    strACCID = "1001";
                                }
                                CwVouitemsEntity2.ACCID = strACCID.Trim();//科目贷码
                                ls = "select BANKNAME from sys_bank where gid='" + ds10.Tables[0].Rows[j]["ACCOUNTRMB"].ToString().Trim() + "'";// in (select top 1 ACCOUNTRMB from ch_fee_settlement where BILLNO='" + ds10.Tables[0].Rows[j]["BILLNO"].ToString().Trim() + "')";
                                string strACCNAME = T_ALL_DA.GetStrSQL("BANKNAME", ls);
                                if (strACCNAME.Trim() == "")
                                {
                                    strACCNAME = "现金";
                                }
                                CwVouitemsEntity2.ACCNAME = strACCNAME.Trim();//科目名称
                                //
                                CwVouitemsEntity2.AMTDR = 0;//本位币贷方金额(互斥)
                                CwVouitemsEntity2.AMTCR = Decimal.Parse(ds10.Tables[0].Rows[j]["je"].ToString().Trim());//本位币借方金额(互斥)
                                strJie = strJie + Decimal.Parse(ds10.Tables[0].Rows[j]["je"].ToString().Trim());
                                CwVouitemsEntity2.ISFCY = false;//是否外币
                                CwVouitemsEntity2.FCYNO = "RMB";//外币编号usd
                                CwVouitemsEntity2.FCYDR = 0;//外币借方金额
                                CwVouitemsEntity2.FCYCR = 0;//外币贷方金额
                                CwVouitemsEntity2.ISDEPTACC = false;//核算部门,按规则设置生成
                                CwVouitemsEntity2.ISEMPLACC = false;//核算人员,按规则设置生成
                                CwVouitemsEntity2.ISCORPACC = false;//核算客户,按规则设置生成
                                CwVouitemsEntity2.ISITEMACC = false;//核算项目,按规则设置生成
                                CwVouitemsEntity2.DC = "C";//贷方向
                            }
                            else if (ds10.Tables[0].Rows[j]["CURRENCY"].ToString().Trim() != "RMB")
                            {
                                ls = "select FINANCESOFTCODE from sys_bank where gid='" + ds10.Tables[0].Rows[j]["ACCOUNTUSD"].ToString().Trim() + "'";// in (select top 1 ACCOUNTUSD from ch_fee_settlement where BILLNO='" + ds10.Tables[0].Rows[j]["BILLNO"].ToString().Trim() + "')";
                                string strACCID = T_ALL_DA.GetStrSQL("FINANCESOFTCODE", ls);
                                if (strACCID.Trim() == "")
                                {
                                    strACCID = "1001";
                                }
                                CwVouitemsEntity2.ACCID = strACCID.Trim();//科目贷码
                                ls = "select BANKNAME from sys_bank where gid='" + ds10.Tables[0].Rows[j]["ACCOUNTUSD"].ToString().Trim() + "'";// in (select top 1 ACCOUNTUSD from ch_fee_settlement where BILLNO='" + ds10.Tables[0].Rows[j]["BILLNO"].ToString().Trim() + "')";
                                string strACCNAME = T_ALL_DA.GetStrSQL("BANKNAME", ls);
                                if (strACCNAME.Trim() == "")
                                {
                                    strACCNAME = "现金";
                                }
                                CwVouitemsEntity2.ACCNAME = strACCNAME.Trim();//科目名称
                                //
                                CwVouitemsEntity2.AMTDR = 0;//本位币贷方金额(互斥)
                                CwVouitemsEntity2.AMTCR = Decimal.Parse(ds10.Tables[0].Rows[j]["hj"].ToString().Trim());//本位币借方金额(互斥)
                                strJie = strJie + Decimal.Parse(ds10.Tables[0].Rows[j]["hj"].ToString().Trim());
                                CwVouitemsEntity2.ISFCY = true;//是否外币
                                CwVouitemsEntity2.FCYNO = "USD";//外币编号usd
                                CwVouitemsEntity2.FCYDR = 0;//外币借方金额
                                CwVouitemsEntity2.FCYCR = Decimal.Parse(ds10.Tables[0].Rows[j]["je"].ToString().Trim());//外币贷方金额
                                CwVouitemsEntity2.ISDEPTACC = false;//核算部门,按规则设置生成
                                CwVouitemsEntity2.ISEMPLACC = false;//核算人员,按规则设置生成
                                CwVouitemsEntity2.ISCORPACC = false;//核算客户,按规则设置生成
                                CwVouitemsEntity2.ISITEMACC = false;//核算项目,按规则设置生成
                                CwVouitemsEntity2.DC = "C";//贷方向
                            }
                            CwVouitemsEntity2.CORPID = "";//ds10.Tables[0].Rows[j]["FAPCODE"].ToString().Trim();//客户(系统)
                            CwVouitemsEntity2.CUSTOMERNAME = "";//ds10.Tables[0].Rows[j]["CUSTOMERNAME"].ToString().Trim();//客户(系统)
                            strBPVW = ds.Tables[0].Rows[0]["BPVW"].ToString().Trim();
                        }
                        CwVouitemsEntity2.FCYEXRATE = Decimal.Parse(ds10.Tables[0].Rows[j]["EXCHANGERATE"].ToString().Trim());//汇率
                        dEXCHANGERATE = Decimal.Parse(ds10.Tables[0].Rows[j]["EXCHANGERATE"].ToString().Trim());//汇率
                        CwVouitemsEntity2.QTYUNIT = "";//数量包装单位
                        CwVouitemsEntity2.QTYDR = 0;//借方数量
                        CwVouitemsEntity2.QTYCR = 0;//贷方数量
                        //
                        //ISRPCLIENTNAME	是否收付客户名称
                        //ISRPCOTCODE	是否收付业务编号
                        //ISRPETD	是否收付航期

                        //ISRPMBLNO	是否收付主提单号
                        //ISRPCUSTNO	是否收付委托编号
                        //ISRPVESSEL	是否收付船名
                        //ISRPVOYNO	是否收付航次

                        //string strSelEXPLAN = "";
                        ////if (ds.Tables[0].Rows[0]["ISRPCLIENTNAME"].ToString().Trim() == "True")
                        ////{
                        ////    strISEXPLAN += " " + ds.Tables[0].Rows[0]["CUSTOMERNAME"].ToString().Trim();
                        ////}
                        //if (ds.Tables[0].Rows[0]["ISRPCUSTNO"].ToString().Trim() == "True")
                        //{
                        //    strSelEXPLAN += " and CUSTNO = '" + ds1.Tables[0].Rows[j]["CUSTNO"].ToString().Trim() + "'";
                        //}
                        //if (ds.Tables[0].Rows[0]["ISRPMBLNO"].ToString().Trim() == "True")
                        //{
                        //    strSelEXPLAN += " and MBLNO = '" + ds1.Tables[0].Rows[j]["MBLNO"].ToString().Trim() + "'";
                        //}
                        //if (ds.Tables[0].Rows[0]["ISRPVESSEL"].ToString().Trim() == "True")
                        //{
                        //    strSelEXPLAN += " and VESSEL = '" + ds1.Tables[0].Rows[j]["VESSEL"].ToString().Trim() + "'";
                        //}
                        //if (ds.Tables[0].Rows[0]["ISRPVOYNO"].ToString().Trim() == "True")
                        //{
                        //    strSelEXPLAN += " and VOYNO = '" + ds1.Tables[0].Rows[j]["VOYNO"].ToString().Trim() + "'";
                        //}
                        ////
                        //sSQL = "select top 1 * from VW_ch_fee_settlement_do where gid_s in(" + sGids.Trim() + ") and CUSTOMERNAME='" + ds1.Tables[0].Rows[j]["CUSTOMERNAME"].ToString().Trim() + "'" + strSelEXPLAN;//BILLNO
                        //DataSet ds4 = T_ALL_DA.GetAllSQL(sSQL);
                        //if (ds4 != null)
                        //{
                        //    //ISCLIENTNAME	是否客户名称
                        //    //ISCUSTNO	是否委托编号
                        //    //ISMBLNO	是否主提单号
                        //    //ISVESSEL	是否船名
                        //    //ISVOYNO	是否航次

                        //    //ISCOTCODE	是否业务编号
                        //    //ISETD	是否航期

                        //    strEXPLAN = "";
                        //    if (ds.Tables[0].Rows[0]["ISRPCLIENTNAME"].ToString().Trim() == "True")
                        //    {
                        //        strEXPLAN += " " + ds4.Tables[0].Rows[0]["CUSTOMERNAME"].ToString().Trim();
                        //    }
                        //    if (ds.Tables[0].Rows[0]["ISRPCUSTNO"].ToString().Trim() == "True")
                        //    {
                        //        strEXPLAN += " " + ds4.Tables[0].Rows[0]["CUSTNO"].ToString().Trim();
                        //    }
                        //    if (ds.Tables[0].Rows[0]["ISRPMBLNO"].ToString().Trim() == "True")
                        //    {
                        //        strEXPLAN += " " + ds4.Tables[0].Rows[0]["MBLNO"].ToString().Trim();
                        //    }
                        //    if (ds.Tables[0].Rows[0]["ISRPVESSEL"].ToString().Trim() == "True")
                        //    {
                        //        strEXPLAN += " " + ds4.Tables[0].Rows[0]["VESSEL"].ToString().Trim();
                        //    }
                        //    if (ds.Tables[0].Rows[0]["ISRPVOYNO"].ToString().Trim() == "True")
                        //    {
                        //        strEXPLAN += " " + ds4.Tables[0].Rows[0]["VOYNO"].ToString().Trim();
                        //    }
                        //}
                        //
                        CwVouitemsEntity2.EXPLAN = strEXPLANALL;//摘要,规则生成
                        CwVouitemsEntity2.DEPTID = "";//strDEPTID;//部门(系统)
                        CwVouitemsEntity2.EMPLID = "";//strEMPLID;//人员(系统)
                        CwVouitemsEntity2.SALE = "";//strSALE;//人员名称
                        CwVouitemsEntity2.PITEMID = 0;//父级项目编号
                        CwVouitemsEntity2.ITEMID = "0";//项目编号
                        CwVouitemsEntity2.FIELD1 = "";//预留字段1
                        CwVouitemsEntity2.FIELD2 = "";//预留字段2
                        CwVouitemsEntity2.FIELD3 = "";//预留字段3
                        CwVouitemsEntity2.FIELD4 = "";//预留字段4
                        CwVouitemsEntity2.FIELD5 = "";//预留字段5
                        CwVouitemsEntity2.FIELD6 = "";//预留字段6
                        CwVouitemsEntity2.FIELD7 = "";//预留字段7
                        CwVouitemsEntity2.FIELD8 = "";//预留字段8
                        CwVouitemsEntity2.FIELD9 = "";//预留字段9
                        CwVouitemsEntity2.FIELD10 = "";//预留字段10
                        CwVouitemsEntity2.ISSYS = true;//是否手动录入
                        CwVouitemsEntity2.FSETTLCODE = "";//结算方式
                        CwVouitemsEntity2.FSETTLENO = "";//财务辅助编号
                        CwVouitemsEntity2.MODIFIEDUSER = strUserID;//最后一次更新操作人
                        CwVouitemsEntity2.BLNO = strBILLNOALL;//BILLNO
                        //if (ds.Tables[0].Rows[0]["ISRPMBLNO"].ToString().Trim() == "True")
                        //{
                        //    CwVouitemsEntity2.BLNO = ds1.Tables[0].Rows[j]["mblno"].ToString().Trim();//BILLNO
                        //}
                        CwVouitemsEntities.Add(CwVouitemsEntity2);
                    }
                }

                //插入主表
                string rq = T_ALL_DA.GetStrSQL("rq", "select rq=getdate()"); 
                CwVouchersEntity CwVouchersEntity = new CwVouchersEntity();
                CwVouchersEntity.ORDNO = strORDNO.Trim();//唯一编码 newid()	varchar	(36)
                CwVouchersEntity.VOUDATE = DateTime.Parse(rq.Trim());//凭证日期="";//datetime="";// 
                CwVouchersEntity.ACCYEAR = rq.Trim().Substring(0, 4);//年="";//int="";// 
                int iM1 = rq.Trim().IndexOf("-") + 1;
                int iM2 = rq.Trim().LastIndexOf("-");
                string strACCMONTH = "";
                if (iM2 - iM1 == 1)
                {
                    strACCMONTH = "0" + rq.Trim().Substring(iM1, iM2 - iM1);
                }
                else
                {
                    strACCMONTH = rq.Trim().Substring(iM1, iM2 - iM1);
                }
                string strVOUNO = "";
                strVOUNO = getVOUNO(rq.Trim().Substring(0, 4), strACCMONTH);
                CwVouchersEntity.VOUNO = strVOUNO;//月初清零	varchar	(16)
                CwVouchersEntity.ACCMONTH = strACCMONTH;//月="";//int="";// 
                string strVOUALLNO = rq.Trim().Substring(0, 4) + strACCMONTH + strVOUNO;
                CwVouchersEntity.VOUALLNO = strVOUALLNO;//月初清零	varchar	(16)
                CwVouchersEntity.VKNO = strBPVW.Trim();//凭证字="";//varchar="";//(8)
                CwVouchersEntity.ATTACHS = int.Parse(ds.Tables[0].Rows[0]["AFFIXNO"].ToString().Trim());//附件(单据)数="";//int="";// 
                CwVouchersEntity.AMTDR = strJie;// iAMTDR;//本位币借方="";//numeric(18, 2)="";// 
                CwVouchersEntity.AMTCR = strDai;//iAMTDR;// iAMTCR;//本位币贷方="";//numeric(18, 2)="";// 
                CwVouchersEntity.FCY = "";//是否含有外币科目="";//varchar="";//(1)
                CwVouchersEntity.QTY = "";//是否含有数量科目="";//varchar="";//(1)
                CwVouchersEntity.VOUPROP = "";//凭证属性="";//varchar="";//(1)
                //CwVouchersEntity.PREPARED = T_ALL_DA.GetStrSQL("FINANCESOFTCODE", "select top 1 FINANCESOFTCODE from user_baseinfo where USERID in (select top 1 gid from [user] where SHOWNAME='" + this.hd_comboPREPARED.Value.Trim() + "' and ISDELETED=0)");//制单人="";//varchar="";//(10)
                CwVouchersEntity.PREPARED = T_ALL_DA.GetStrSQL("SHOWNAME", "select SHOWNAME from [user] where gid='" + strUserID.Trim() + "' and ISDELETED=0"); ;//制单人="";//varchar="";//(10)
                CwVouchersEntity.CHECKED = "";//审核人="";//varchar="";//(10)
                CwVouchersEntity.ENTERED = "";//记账人="";//varchar="";//(10)
                CwVouchersEntity.ERRMSG = "";//错误信息="";//varchar="";//(6)
                CwVouchersEntity.ISDELETE = false;//是否废除="";//bit="";//Y
                CwVouchersEntity.DELETEUSER = "";//废除操作人="";//Varchar(36)="";//Y
                //CwVouchersEntity.DELETETIME="";//废除时间="";//Datetime="";//Y
                //
                //生成入库
                string strSel = " and gid_s in(" + sGids + ") " + strVESSEL;
                iResult = CwVouchersDA.setInsertAll(CwVouchersEntity, CwVouitemsEntities, "VW_ch_fee_settlement_do", strVOUALLNO, strSel, strUserID, 2);
            }
            else
            {
                return "-1";
            }
            return strORDNO.Trim();
        }

        protected String getVOUNO(string strY, string strM)
        {
            string sVOUNO = "";
            string strVOUNO = "select top 1 VOUNO from cw_vouchers where ACCYEAR='" + strY + "' and ACCMONTH='" + strM + "' order by convert(decimal(10),VOUNO) desc";
            strVOUNO = T_ALL_DA.GetStrSQL("VOUNO", strVOUNO);
            if (strVOUNO.Trim() == "")
            {
                strVOUNO = "0001";
            }
            else
            {
                sVOUNO = (int.Parse(strVOUNO) + 1).ToString().Trim();
                strVOUNO = sVOUNO;
                for (int i = 0; i < 4 - sVOUNO.Length; i++)
                {
                    strVOUNO = "0" + strVOUNO;
                }
            }
            return strVOUNO;
        }
    }
}