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 System.Collections.Generic;
using JsonHelper;
using DSWeb.EntityDA;
using DSWeb.Log;

namespace DSWeb.CW
{
    public partial class CwVouchersAddDo : System.Web.UI.Page
    {
        private string recvJSON;
        RangeDA RangeDA = new RangeDA();
        T_ALL_DA T_ALL_DA = new T_ALL_DA();
        public string strdate;//用户GID
        public string strUserID;//登录用户GID
        public string strCompanyID;//公司GID
        public string strShowName;//用户显示名
        public string strDeptName;//部门名称

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USERID"] != null)
            {
                strUserID = Session["USERID"].ToString().Trim();
                this.hdUserID.Value = strUserID;
            }
            else
            {
                Server.Transfer("~/Error/FriendError.aspx");
                return;
                //strUserID = "1BEC90E1-9780-472F-90C2-0C6390C044A4";
                //this.hdUserID.Value = strUserID; 
            }
            if (Session["SHOWNAME"] != null)
            {
                strShowName = Session["SHOWNAME"].ToString();
            }
            if (Session["COMPANYID"] != null)
            {
                strCompanyID = Session["COMPANYID"].ToString();
            }
            if (Session["DEPTNAME"] != null)
            {
                strDeptName = Session["DEPTNAME"].ToString();
            }
            //
            T_ALL_DA T_ALL_DA = new T_ALL_DA();
            strdate = T_ALL_DA.GetStrSQL("rq", "select convert(char(10),getdate(),20) as rq");
            //
            if (Request.QueryString["ordno"] != null)
            {
                this.hdOrdno.Value = Request.QueryString["ordno"].ToString();
            }

            //
            if (!IsPostBack)
            {
                CwVouchersDA CwVouchersDA = new CwVouchersDA();
                CwVouchersEntity CwVouchersEntity = new CwVouchersEntity();
                if (this.hdOrdno.Value.Trim() != "")
                {
                    CwVouchersEntity = CwVouchersDA.GetModel(this.hdOrdno.Value.Trim());
                    this.hd_comboPREPARED.Value = CwVouchersEntity.PREPARED.ToString().Trim();
                    tbVOUDATE.Text = CwVouchersEntity.VOUDATE.ToString("yyyy-MM-dd").Trim();
                    tbRATE.Text = "";
                    this.hdVOUALLNO.Value = CwVouchersEntity.VOUALLNO.ToString().Trim();
                }
            }

            //
            recvJSON = "";
            recvJSON = this.recvContainer.Value.Trim();


            /*
            //权限可视范围
            this.hdRangeV.Value = RangeDA.GetVISIBLERANGE(strUserID.Trim(), "modOrderManagement");
            this.hdRangeO.Value = RangeDA.GetOPERATERANGE(strUserID.Trim(), "modOrderManagement");
            string alt = "";
            if (this.hdRangeV.Value.Trim() != this.hdRangeO.Value.Trim())
            {
                if (this.hdRangeO.Value.Trim().Equals("0"))//全部
                {
                }
                else if (this.hdRangeO.Value.Trim().Equals("1"))//分公司
                {
                    alt = RangeDA.GetOrderManagementCompany(strUserID.Trim(), strSeaBSNO);
                    if (alt == "")
                    {
                        divadd.Disabled = true;
                        divsave.Disabled = true;
                        divdel.Disabled = true;
                        divrollback.Disabled = true;
                        mygrid_container_recv.Disabled = true;
                    }
                }
                else if (this.hdRangeO.Value.Trim().Equals("2"))//部门
                {
                    alt = RangeDA.GetOrderManagementDEPT(strUserID.Trim(), strSeaBSNO);
                    if (alt == "")
                    {
                        divadd.Disabled = true;
                        divsave.Disabled = true;
                        divdel.Disabled = true;
                        divrollback.Disabled = true;
                        mygrid_container_recv.Disabled = true;
                    }
                }
                else if (this.hdRangeO.Value.Trim().Equals("3"))//个人
                {
                    alt = RangeDA.GetOrderManagementPerson(strUserID.Trim(), strSeaBSNO);
                    if (alt == "")
                    {
                        divadd.Disabled = true;
                        divsave.Disabled = true;
                        divdel.Disabled = true;
                        divrollback.Disabled = true;
                        mygrid_container_recv.Disabled = true;
                    }
                }
                else if (this.hdRangeO.Value.Trim().Equals("4"))//无
                {
                    divadd.Disabled = true;
                    divsave.Disabled = true;
                    divdel.Disabled = true;
                    divrollback.Disabled = true;
                    mygrid_container_recv.Disabled = true;
                }
            }
            */
            //
            if (!recvJSON.Trim().Equals(""))
            {
                //应收账户保存
                JsonCwVouchersAddDoGroupEntity CwVouchersAddDoGroupRecvEntity = GetPostEntity(recvJSON);
                List<JsonCwVouchersAddDoEntity> CwVouchersAddDoRecvEntities = new List<JsonCwVouchersAddDoEntity>();

                CwVouchersAddDoRecvEntities = CwVouchersAddDoGroupRecvEntity.JsonCwVouchersAddDoEntities;

                SaveRecvCwVouchersAddDo(CwVouchersAddDoGroupRecvEntity);
                //更新完初始化
                recvJSON = "";
            }
            //加载下拉框
            Page.ClientScript.RegisterStartupScript(this.GetType(), "key0", "<script>initcomboPREPARED();</script>");            
        }

        
        #region 保存应收账户列表更改结果
        /// <summary>
        /// 保存应收账户列表更改结果
        /// </summary>
        /// <param name="tempGroupEntity"></param>
        private void SaveRecvCwVouchersAddDo(JsonCwVouchersAddDoGroupEntity tempGroupEntity)
        {
            if (tempGroupEntity != null)
            {
                T_ALL_DA T_ALL_DA = new T_ALL_DA();
                //帐套设置
                string sSQL = "SELECT top 1 * from cw_design";
                DataSet ds = T_ALL_DA.GetAllSQL(sSQL);
                //其他常用科目设置 应收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);
                //
                List<CwVouitemsEntity> CwVouitemsEntities = new List<CwVouitemsEntity>();
                List<JsonCwVouchersAddDoEntity> CwVouchersAddDoEntities = new List<JsonCwVouchersAddDoEntity>();
                CwVouchersAddDoEntities = tempGroupEntity.JsonCwVouchersAddDoEntities;
                DataTable CwVouchersAddDoTable = new DataTable();
                //
                ArrayList sqlInsertList = new ArrayList();
                CwVouitemsDA CwVouitemsDA = new CwVouitemsDA();
                CwVouitemsEntity CwVouitemsEntityGID = new CwVouitemsEntity();
                                
                //更新明细表
                string strInitSql = "";
                if (tempGroupEntity.JsonCwVouchersAddDoEntities.Count > 0)
                {
                    strInitSql = " SELECT GID,ITEMNO,EXPLAN,ACCID,ACCNAME,AMTDR,AMTCR,FCYDR,FCYCR,FCYEXRATE,CUSTOMERNAME,CORPID,SALE,BLNO,MODIFIEDUSER,MODIFIEDTIME FROM cw_vouitems WHERE 1=1 and ORDNO='" + this.hdOrdno.Value.Trim() + "' ORDER BY ITEMNO";
                }
                else
                {
                    return;
                }

                CwVouchersAddDoTable = CwVouitemsDA.GetExcuteSql(strInitSql).Tables[0];

                //更新操作SQL语句
                ArrayList sqlList = new ArrayList();
                //日志操作SQL语句
                ArrayList logList = new ArrayList();
                //日志记录
                Logger log = Logger.Instance;

                for (int i = 0; i < CwVouchersAddDoTable.Rows.Count; i++)
                {
                    foreach (JsonCwVouchersAddDoEntity CwVouchersAddDo in CwVouchersAddDoEntities)
                    {
                        if (CwVouchersAddDoTable.Rows[i]["GID"].ToString().Trim().Equals(CwVouchersAddDo.CwVouchersAddDo1.Trim()))//惟一编号
                        {
                            CwVouitemsEntity model = new CwVouitemsEntity();
                            string strSql = "";
                            string LogContent = "";
                            LogEntity logEntity = new LogEntity();
                            //GID
                            string strGID = CwVouchersAddDo.CwVouchersAddDo1.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo1.ToString().Trim();
                            model.GID = strGID;
                            //
                            #region 更改前后信息对比
                            //行号
                            string stritemno = CwVouchersAddDo.CwVouchersAddDo2.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo2.ToString().Trim();
                            model.ITEMNO = int.Parse(stritemno);
                            if (!CwVouchersAddDoTable.Rows[i]["itemno"].ToString().Trim().Equals(stritemno))
                            {
                                //strSql += String.Format(" itemno = '{0}'", stritemno);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "行号", CwVouchersAddDoTable.Rows[i]["itemno"].ToString().Trim(), stritemno);
                            }
                            //摘要
                            string strexplan = CwVouchersAddDo.CwVouchersAddDo3.Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo3.Trim();
                            model.EXPLAN = strexplan;
                            if (!CwVouchersAddDoTable.Rows[i]["explan"].ToString().Trim().Equals(strexplan))
                            {
                                //strSql += String.Format(",explan = '{0}'", strexplan);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "摘要", CwVouchersAddDoTable.Rows[i]["explan"].ToString().Trim(), strexplan);
                            }
                            //科目代码
                            string straccid = CwVouchersAddDo.CwVouchersAddDo4.Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo4.Trim();
                            model.ACCID = straccid;
                            if (!CwVouchersAddDoTable.Rows[i]["accid"].ToString().Trim().Equals(straccid))
                            {
                                //strSql += String.Format(",accid = '{0}'", straccid);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "科目代码", CwVouchersAddDoTable.Rows[i]["accid"].ToString().Trim(), straccid);
                            }
                            //科目名称
                            string straccname = CwVouchersAddDo.CwVouchersAddDo5.ToString().Trim().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo5.ToString().Trim();
                            model.ACCNAME = straccname;
                            if (!CwVouchersAddDoTable.Rows[i]["accname"].ToString().Trim().Equals(straccname))
                            {
                                //strSql += String.Format(",accname = '{0}'", straccname);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "科目名称", CwVouchersAddDoTable.Rows[i]["accname"].ToString().Trim(), straccname);
                            }
                            //本位币借方金额
                            string stramtdr = CwVouchersAddDo.CwVouchersAddDo6.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo6.ToString("F2").Trim();
                            //外币借方金额
                            string strfcydr = CwVouchersAddDo.CwVouchersAddDo8.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo8.ToString("F2").Trim();
                            //汇率
                            string strfcyexrate = CwVouchersAddDo.CwVouchersAddDo10.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo10.ToString("F2").Trim();
                            //if (tbRATE.Text.Trim() != "" && !cbISRATE.Checked)
                            //{
                            //    Decimal diRATE = Decimal.Parse(tbRATE.Text.Trim());
                            //    if (diRATE > 0)
                            //    {
                            //        strfcyexrate = tbRATE.Text.Trim();
                            //    }
                            //}
                            if (strfcydr.Trim() != "")
                            {
                                Decimal difcydr = Decimal.Parse(strfcydr);
                                if (tbRATE.Text.Trim() != "" && difcydr > 0 && !cbISRATE.Checked)
                                {
                                    Decimal diRATE = Decimal.Parse(tbRATE.Text.Trim());
                                    difcydr = diRATE * difcydr;
                                    stramtdr = difcydr.ToString("F2").Trim();
                                    strfcyexrate = diRATE.ToString().Trim();
                                }
                            }
                            //
                            model.AMTDR = Decimal.Parse(stramtdr);
                            if (!CwVouchersAddDoTable.Rows[i]["amtdr"].ToString().Trim().Equals(stramtdr))
                            {
                                //strSql += String.Format(",amtdr = '{0}',dc='D',isfcy=0,FCYNO='RMB'", stramtdr);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "本位币借方金额", CwVouchersAddDoTable.Rows[i]["amtdr"].ToString().Trim(), stramtdr);
                            }
                            //本位币贷方金额
                            string stramtcr = CwVouchersAddDo.CwVouchersAddDo7.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo7.ToString("F2").Trim();
                            //外币贷方金额
                            string strfcycr = CwVouchersAddDo.CwVouchersAddDo9.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo9.ToString("F2").Trim();
                            //
                            if (strfcycr.Trim() != "")
                            {
                                Decimal difcycr = Decimal.Parse(strfcycr);
                                if (tbRATE.Text.Trim() != "" && difcycr > 0 && !cbISRATE.Checked)
                                {
                                    Decimal diRATE = Decimal.Parse(tbRATE.Text.Trim());
                                    difcycr = diRATE * difcycr;
                                    stramtcr = difcycr.ToString().Trim();
                                    strfcyexrate = diRATE.ToString().Trim();
                                }
                            }
                            //
                            model.AMTCR = Decimal.Parse(stramtcr);
                            if (!CwVouchersAddDoTable.Rows[i]["amtcr"].ToString().Trim().Equals(stramtcr))
                            {
                                //strSql += String.Format(",amtcr = '{0}',dc='C',isfcy=0,FCYNO='RMB'", stramtcr);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "本位币贷方金额", CwVouchersAddDoTable.Rows[i]["amtcr"].ToString().Trim(), stramtcr);
                            }
                            //外币借方金额
                            model.FCYDR = Decimal.Parse(strfcydr);
                            if (!CwVouchersAddDoTable.Rows[i]["fcydr"].ToString().Trim().Equals(strfcydr))
                            {
                                //strSql += String.Format(",fcydr = '{0}',dc='D',isfcy=1,FCYNO='USD'", strfcydr);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "外币借方金额", CwVouchersAddDoTable.Rows[i]["fcydr"].ToString().Trim(), strfcydr);
                            }
                            //外币贷方金额
                            model.FCYCR = Decimal.Parse(strfcycr);
                            if (!CwVouchersAddDoTable.Rows[i]["fcycr"].ToString().Trim().Equals(strfcycr))
                            {
                                //strSql += String.Format(",fcycr = '{0}',dc='C',isfcy=1,FCYNO='USD'", strfcycr);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "外币贷方金额", CwVouchersAddDoTable.Rows[i]["fcycr"].ToString().Trim(), strfcycr);
                            }
                            //汇率
                            Decimal difcyexrate = Decimal.Parse(strfcyexrate.Trim());
                            if (difcyexrate < 1)
                            {
                                strfcyexrate = "1.00";
                            }
                            model.FCYEXRATE = Decimal.Parse(strfcyexrate);
                            if (!CwVouchersAddDoTable.Rows[i]["fcyexrate"].ToString().Trim().Equals(strfcyexrate))
                            {
                                //strSql += String.Format(",fcyexrate = '{0}'", strfcyexrate);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "汇率", CwVouchersAddDoTable.Rows[i]["fcyexrate"].ToString().Trim(), strfcyexrate);
                            }
                            //客户名称
                            string strcustomername = CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim();
                            model.CUSTOMERNAME = strcustomername;
                            if (!CwVouchersAddDoTable.Rows[i]["customername"].ToString().Trim().Equals(strcustomername))
                            {
                                //strSql += String.Format(",customername = '{0}'", strcustomername);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "客户名称", CwVouchersAddDoTable.Rows[i]["customername"].ToString().Trim(), strcustomername);
                            }
                            //客户(系统)
                            string strcorpid = CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim();
                            model.CORPID = strcorpid;
                            if (!CwVouchersAddDoTable.Rows[i]["corpid"].ToString().Trim().Equals(strcorpid))
                            {
                                //strSql += String.Format(",corpid = '{0}'", strcorpid);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "客户(系统)", CwVouchersAddDoTable.Rows[i]["corpid"].ToString().Trim(), strcorpid);
                            }
                            //揽货人名称
                            string strsale = CwVouchersAddDo.CwVouchersAddDo13.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo13.ToString().Trim();
                            string ls = "select top 1 FINANCESOFTCODE from user_baseinfo where USERID in (select gid from [user] where ISDELETED=0 and SHOWNAME='" + strsale + "')";
                            string stremplid = T_ALL_DA.GetStrSQL("FINANCESOFTCODE", ls);
                            model.SALE = strsale;
                            model.EMPLID = stremplid;
                            if (!CwVouchersAddDoTable.Rows[i]["sale"].ToString().Trim().Equals(strsale))
                            {
                                //strSql += String.Format(",sale = '{0}',emplid = '{1}'", strsale, stremplid);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "揽货人名称", CwVouchersAddDoTable.Rows[i]["sale"].ToString().Trim(), strsale);
                            }
                            //业务编号
                            string strblno = CwVouchersAddDo.CwVouchersAddDo14.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo14.ToString().Trim();
                            model.BLNO = strblno;
                            if (!CwVouchersAddDoTable.Rows[i]["blno"].ToString().Trim().Equals(strblno))
                            {
                                //strSql += String.Format(",blno = '{0}'", strblno);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "业务编号", CwVouchersAddDoTable.Rows[i]["blno"].ToString().Trim(), strblno);
                            }
                            //
                            model.MODIFIEDUSER = strUserID;
                            if (strSql.Trim() != "")
                            {
                                //最后一次更改操作人
                                //strSql += String.Format(",MODIFIEDUSER = '{0}'", strUserID);
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "最后一次更改操作人", CwVouchersAddDoTable.Rows[i]["MODIFIEDUSER"].ToString().Trim(), strUserID);
                                //最后一次更改操作时间
                                //strSql += ",MODIFIEDTIME = getdate()";
                                LogContent += String.Format(log.GetLogContentTemplate(Logger.LogTypes.UPDATE), "最后一次更改操作时间", CwVouchersAddDoTable.Rows[i]["MODIFIEDTIME"].ToString().Trim(), DateTime.Now.ToString().Trim());
                            }
                            #endregion

                            //如果有字段内容更新则执行更新操作
                            if (LogContent.Trim().Length > 1)
                            {
                                //if (strSql.Trim().StartsWith(","))
                                //{
                                //    strSql = strSql.Trim().Substring(1);
                                //}
                                //string strUpdateSql = String.Format("UPDATE {0} SET {1} WHERE GID = '{2}'", "cw_vouitems", strSql, CwVouchersAddDo.CwVouchersAddDo1.Trim());
                                //sqlList.Add(strUpdateSql);
                                //
                                //插入明细表
                                if ((strfcycr.Trim() != "0" && strfcycr.Trim() != "" && strfcycr.Trim() != "0.00") || (strfcydr.Trim() != "0" && strfcydr.Trim() != "" && strfcydr.Trim() != "0.00"))
                                {
                                    model.ISFCY = true;
                                    model.FCYNO = "USD";
                                }
                                else
                                {
                                    model.ISFCY = false;
                                    model.FCYNO = "RMB";
                                }
                                //
                                if (ds.Tables[0].Rows[0]["AR"].ToString().Trim() == model.ACCID.ToString().Trim())//应收
                                {
                                    if (model.FCYNO.ToString().Trim() == "RMB")
                                    {
                                        if (ds2 == null)
                                        {
                                            model.ISDEPTACC = false;//核算部门,按规则设置生成
                                            model.ISEMPLACC = false;//核算人员,按规则设置生成
                                            model.ISCORPACC = false;//核算客户,按规则设置生成
                                            model.ISITEMACC = false;//核算项目,按规则设置生成
                                            model.DC = "D";//贷方向
                                        }
                                        else
                                        {
                                            model.ISDEPTACC = Boolean.Parse(ds2.Tables[0].Rows[0]["ISDEPTACC"].ToString().Trim());//核算部门,按规则设置生成
                                            model.ISEMPLACC = Boolean.Parse(ds2.Tables[0].Rows[0]["ISEMPLACC"].ToString().Trim());//核算人员,按规则设置生成
                                            model.ISCORPACC = Boolean.Parse(ds2.Tables[0].Rows[0]["ISCORPACC"].ToString().Trim());//核算客户,按规则设置生成
                                            model.ISITEMACC = Boolean.Parse(ds2.Tables[0].Rows[0]["ISITEMACC"].ToString().Trim());//核算项目,按规则设置生成
                                            if (ds2.Tables[0].Rows[0]["DC"].ToString().Trim() == "借")
                                            {
                                                model.DC = "D";//借方向
                                            }
                                            else
                                            {
                                                model.DC = "C";//贷方向
                                            }
                                        }
                                    }
                                    else if (model.FCYNO.ToString().Trim() == "USD")
                                    {
                                        if (ds6 == null)
                                        {
                                            model.ISDEPTACC = false;//核算部门,按规则设置生成
                                            model.ISEMPLACC = false;//核算人员,按规则设置生成
                                            model.ISCORPACC = false;//核算客户,按规则设置生成
                                            model.ISITEMACC = false;//核算项目,按规则设置生成
                                            model.DC = "C";//贷方向
                                        }
                                        else
                                        {
                                            model.ISDEPTACC = Boolean.Parse(ds6.Tables[0].Rows[0]["ISDEPTACC"].ToString().Trim());//核算部门,按规则设置生成
                                            model.ISEMPLACC = Boolean.Parse(ds6.Tables[0].Rows[0]["ISEMPLACC"].ToString().Trim());//核算人员,按规则设置生成
                                            model.ISCORPACC = Boolean.Parse(ds6.Tables[0].Rows[0]["ISCORPACC"].ToString().Trim());//核算客户,按规则设置生成
                                            model.ISITEMACC = Boolean.Parse(ds6.Tables[0].Rows[0]["ISITEMACC"].ToString().Trim());//核算项目,按规则设置生成
                                            if (ds6.Tables[0].Rows[0]["DC"].ToString().Trim() == "借")
                                            {
                                                model.DC = "D";//借方向
                                            }
                                            else
                                            {
                                                model.DC = "C";//贷方向
                                            }
                                        }
                                    }
                                    //客户名称
                                    model.CUSTOMERNAME = CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim();
                                    //客户(系统)
                                    model.CORPID = CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim();
                                }
                                else if (ds.Tables[0].Rows[0]["AP"].ToString().Trim() == model.ACCID.ToString().Trim())//应付
                                {
                                    if (model.FCYNO.ToString().Trim() == "RMB")
                                    {
                                        if (ds3 == null)
                                        {
                                            model.CORPID = "";//客户(系统)
                                            model.CUSTOMERNAME = "";//客户(系统)
                                            model.ISDEPTACC = false;//核算部门,按规则设置生成
                                            model.ISEMPLACC = false;//核算人员,按规则设置生成
                                            model.ISCORPACC = false;//核算客户,按规则设置生成
                                            model.ISITEMACC = false;//核算项目,按规则设置生成
                                            model.DC = "C";//贷方向
                                        }
                                        else
                                        {
                                            //客户(系统)
                                            model.CORPID = CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim();
                                            //客户名称
                                            model.CUSTOMERNAME = CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim();
                                            model.ISCORPACC = Boolean.Parse(ds3.Tables[0].Rows[0]["ISCORPACC"].ToString().Trim());//核算客户,按规则设置生成
                                            model.ISDEPTACC = Boolean.Parse(ds3.Tables[0].Rows[0]["ISDEPTACC"].ToString().Trim());//核算部门,按规则设置生成
                                            model.ISEMPLACC = Boolean.Parse(ds3.Tables[0].Rows[0]["ISEMPLACC"].ToString().Trim());//核算人员,按规则设置生成
                                            model.ISITEMACC = Boolean.Parse(ds3.Tables[0].Rows[0]["ISITEMACC"].ToString().Trim());//核算项目,按规则设置生成
                                            if (ds3.Tables[0].Rows[0]["DC"].ToString().Trim() == "借")
                                            {
                                                model.DC = "D";//借方向
                                            }
                                            else
                                            {
                                                model.DC = "C";//贷方向
                                            }
                                        }
                                    }
                                    else if (model.FCYNO.ToString().Trim() == "USD")
                                    {
                                        if (ds7 == null)
                                        {
                                            //客户(系统)
                                            model.CORPID = "";
                                            //客户名称
                                            model.CUSTOMERNAME = "";
                                            model.ISDEPTACC = false;//核算部门,按规则设置生成
                                            model.ISEMPLACC = false;//核算人员,按规则设置生成
                                            model.ISCORPACC = false;//核算客户,按规则设置生成
                                            model.ISITEMACC = false;//核算项目,按规则设置生成
                                            model.DC = "C";//贷方向
                                            model.CORPID = "";//客户(系统)
                                            model.CUSTOMERNAME = "";//客户(系统)
                                        }
                                        else
                                        {
                                            //客户(系统)
                                            model.CORPID = CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim();
                                            //客户名称
                                            model.CUSTOMERNAME = CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim();
                                            model.ISCORPACC = Boolean.Parse(ds7.Tables[0].Rows[0]["ISCORPACC"].ToString().Trim());//核算客户,按规则设置生成
                                            model.ISDEPTACC = Boolean.Parse(ds7.Tables[0].Rows[0]["ISDEPTACC"].ToString().Trim());//核算部门,按规则设置生成
                                            model.ISEMPLACC = Boolean.Parse(ds7.Tables[0].Rows[0]["ISEMPLACC"].ToString().Trim());//核算人员,按规则设置生成
                                            model.ISITEMACC = Boolean.Parse(ds7.Tables[0].Rows[0]["ISITEMACC"].ToString().Trim());//核算项目,按规则设置生成
                                            if (ds7.Tables[0].Rows[0]["DC"].ToString().Trim() == "借")
                                            {
                                                model.DC = "D";//借方向
                                            }
                                            else
                                            {
                                                model.DC = "C";//贷方向
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    //客户(系统)
                                    model.CORPID = "";
                                    //客户名称
                                    model.CUSTOMERNAME = "";
                                    model.ISDEPTACC = false;//核算部门,按规则设置生成
                                    model.ISEMPLACC = false;//核算人员,按规则设置生成
                                    model.ISCORPACC = false;//核算客户,按规则设置生成
                                    model.ISITEMACC = false;//核算项目,按规则设置生成
                                    model.DC = "C";//贷方向
                                    model.CORPID = "";//客户(系统)
                                    model.CUSTOMERNAME = "";//客户(系统)
                                }
                                //
                                model.ORDNO = this.hdOrdno.Value.Trim();
                                model.ISSYS = true;
                                //
                                model.QTYUNIT = "";
                                model.QTYDR = 0;
                                model.QTYCR = 0;
                                // model.ISDEPTACC = false;
                                //model.ISEMPLACC = false;
                                //model.ISCORPACC = false;
                                //model.ISITEMACC = false;
                                model.DEPTID = "";
                                model.EMPLID = "";
                                model.PITEMID = 0;
                                model.ITEMID = "0";
                                model.FIELD10 = "";
                                model.FIELD9 = "";
                                model.FIELD8 = "";
                                model.FIELD7 = "";
                                model.FIELD6 = "";
                                model.FIELD5 = "";
                                model.FIELD4 = "";
                                model.FIELD3 = "";
                                model.FIELD2 = "";
                                model.FIELD1 = "";
                                model.FSETTLCODE = "";
                                model.FSETTLENO = "";
                                model.PACCID = "";
                                model.PROPERTY = 0;
                                model.GRADE = 0;
                                //model.DC = "";
                                //
                                CwVouitemsEntities.Add(model);
                                logList.Add(String.Format("更新账户标识 {0} {1}", CwVouchersAddDo.CwVouchersAddDo1.Trim(), LogContent));
                            }
                            CwVouchersAddDoEntities.Remove(CwVouchersAddDo);
                            break;
                        }
                    }

                }
                if (CwVouitemsEntities.Count > 0)
                {
                    int iResult = CwVouitemsDA.UpdateFromGrid(CwVouitemsEntities);
                    //为1表示更新成功
                    //为-1有异常,更新失败
                    //为-2更新异常,事务已回滚成功
                    //更新完成后,记录日志
                    if (iResult == 1)
                    {
                        #region 日志记录操作
                        // INSERT-"INSERT INTO {0} ({1}) VALUES ({2})"    @{0}-所要插入记录的表;{1}记录字段值;{2}-记录新值@
                        //if (logList.Count > 0)
                        //{
                        //    for (int i = 0; i < logList.Count; i++)
                        //    {
                        //        LogEntity logEntity = new LogEntity();
                        //        logEntity.GID = Guid.NewGuid().ToString();
                        //        logEntity.Name = "更新账户信息列表";
                        //        logEntity.LogType = "UPDATE 更新操作";
                        //        logEntity.CreateUserID = strUserID.Trim();//测试使用数据,正式发布请删除
                        //        logEntity.LogContent = logList[i].ToString();
                        //        log.WriteLog(logEntity);
                        //    }
                        //}
                        #endregion
                    }
                }

                //更新主表
                int iResult1 = 0;
                CwVouchersDA CwVouchersDA = new CwVouchersDA();
                CwVouchersEntity CwVouchersEntity = new CwVouchersEntity();
                //
                CwVouchersEntity.ORDNO = this.hdOrdno.Value.Trim();
                //
                string rq = tbVOUDATE.Text.Trim();
                CwVouchersEntity.VOUDATE = DateTime.Parse(rq);
                CwVouchersEntity.PREPARED = this.hd_comboPREPARED.Value.Trim();
                CwVouchersEntity.ACCYEAR = rq.Trim().Substring(0, 4);//年
                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;//月初清零
                CwVouchersEntity.ACCMONTH = strACCMONTH;//月
                string strVOUALLNO = rq.Trim().Substring(0, 4) + strACCMONTH + strVOUNO;
                CwVouchersEntity.VOUALLNO = strVOUALLNO;//
                if (strVOUALLNO.Trim() != this.hdVOUALLNO.Value.Trim())
                {
                    iResult1 = CwVouchersDA.UpdateOPDate(CwVouchersEntity, this.hdVOUALLNO.Value.Trim());
                }
                //
                string strAMTDR = T_ALL_DA.GetStrSQL("AMTDR", "select sum(AMTDR) as AMTDR from cw_vouitems where ORDNO='" + this.hdOrdno.Value.Trim() + "'");
                string strAMTCR = T_ALL_DA.GetStrSQL("AMTCR", "select sum(AMTCR) as AMTCR from cw_vouitems where ORDNO='" + this.hdOrdno.Value.Trim() + "'");
                //
                if ((strAMTDR.Trim() != "0" && strAMTDR.Trim() != "" && strAMTDR.Trim() != "0.00"))
                {
                    CwVouchersEntity.AMTDR = Decimal.Parse(Decimal.Parse(strAMTDR.Trim()).ToString("F2"));
                }
                else
                {
                    CwVouchersEntity.AMTDR = 0;
                }
                //
                if ((strAMTCR.Trim() != "0" && strAMTCR.Trim() != "" && strAMTCR.Trim() != "0.00"))
                {
                    CwVouchersEntity.AMTCR = Decimal.Parse(Decimal.Parse(strAMTCR.Trim()).ToString("F2"));
                }
                else
                {
                    CwVouchersEntity.AMTDR = 0;
                }
                //
                iResult1 = CwVouchersDA.UpdateAMT(CwVouchersEntity);
                //
                //如果遍历完所有币别表数据,没有相对应的币别信息,则将剩余的币别信息插入到币别表中
                if (CwVouchersAddDoEntities.Count > 0)
                {
                    //插入新的应收币别信息
                    InsertCwVouchersAddDo(CwVouchersAddDoEntities);
                }
                
                //
                recvJSON = "";
                this.recvContainer.Value = "";
            }
            else
            {
                recvJSON = "";
                this.recvContainer.Value = "";
                return;
            }
        }
        #endregion

        #region 插入账户信息记录
        /// <summary>
        /// 插入账户信息记录
        /// </summary>
        /// <param name="CwVouchersAddDoEntities">需要插入账户表的账户实体类</param>
        public void InsertCwVouchersAddDo(List<JsonCwVouchersAddDoEntity> CwVouchersAddDoEntities)
        {        
            T_ALL_DA T_ALL_DA = new T_ALL_DA();
            //帐套设置
            string sSQL = "SELECT top 1 * from cw_design";
            DataSet ds = T_ALL_DA.GetAllSQL(sSQL);
            //其他常用科目设置 应收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);
            //
            ArrayList sqlInsertList = new ArrayList();
            CwVouitemsDA CwVouitemsDA = new CwVouitemsDA();
            CwVouitemsEntity CwVouitemsEntityGID = new CwVouitemsEntity();
            string alt = "";

            foreach (JsonCwVouchersAddDoEntity CwVouchersAddDo in CwVouchersAddDoEntities)
            {
                CwVouitemsEntity model = new CwVouitemsEntity();
                model.GID = CwVouchersAddDo.CwVouchersAddDo1.Trim().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo1.Trim();
                //本位币借方金额
                string stramtdr = CwVouchersAddDo.CwVouchersAddDo6.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo6.ToString().Trim();
                if (stramtdr.Trim() == "" || stramtdr.Trim() == "0" || stramtdr.Trim() == "0.00")
                {
                    model.AMTDR = 0;
                }
                else
                {
                    model.AMTDR = Decimal.Parse(Decimal.Parse(stramtdr.Trim()).ToString("F2"));
                    model.AMTCR = 0;
                }
                //本位币贷方金额
                string stramtcr = CwVouchersAddDo.CwVouchersAddDo7.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo7.ToString().Trim();
                if (stramtcr.Trim() == "" || stramtcr.Trim() == "0" || stramtcr.Trim() == "0.00")
                {
                    model.AMTCR = 0;
                }
                else
                {
                    model.AMTCR = Decimal.Parse(Decimal.Parse(stramtcr.Trim()).ToString("F2"));
                    model.AMTDR = 0;
                }
                //汇率
                string strfcyexrate = CwVouchersAddDo.CwVouchersAddDo10.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo10.ToString().Trim();                            
                //外币借方金额
                string strfcydr = CwVouchersAddDo.CwVouchersAddDo8.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo8.ToString().Trim();
                if ((strfcydr.Trim() != "0" && strfcydr.Trim() != "" && strfcydr.Trim() != "0.00") && (strfcyexrate.Trim() != "" || strfcyexrate.Trim() != "0" || strfcyexrate.Trim() != "0.00"))
                {
                    stramtdr = (Decimal.Parse(strfcydr.Trim()) * Decimal.Parse(strfcyexrate.Trim())).ToString("F2");
                    model.AMTDR = Decimal.Parse((Decimal.Parse(strfcydr.Trim()) * Decimal.Parse(strfcyexrate.Trim())).ToString("F2"));
                    stramtcr = "0";
                    model.AMTCR = 0;
                }
                //外币贷方金额
                string strfcycr = CwVouchersAddDo.CwVouchersAddDo9.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo9.ToString().Trim();
                if ((strfcycr.Trim() != "0" && strfcycr.Trim() != "" && strfcycr.Trim() != "0.00") && (strfcyexrate.Trim() != "" || strfcyexrate.Trim() != "0" || strfcyexrate.Trim() != "0.00"))
                {
                    stramtcr = (Decimal.Parse(strfcycr.Trim()) * Decimal.Parse(strfcyexrate.Trim())).ToString("F2");
                    model.AMTCR = Decimal.Parse((Decimal.Parse(strfcycr.Trim()) * Decimal.Parse(strfcyexrate.Trim())).ToString("F2"));
                    stramtdr = "0";
                    model.AMTDR = 0;
                }
                //汇率
                if (strfcyexrate.Trim() == "" || strfcyexrate.Trim() == "0" || strfcyexrate.Trim() == "0.00")
                {
                    model.FCYEXRATE = 1;
                }
                else
                {
                    model.FCYEXRATE = Decimal.Parse(Decimal.Parse(strfcyexrate.Trim()).ToString("F2"));
                }
                //
                if (strfcydr.Trim() == "" || strfcydr.Trim() == "0" || strfcydr.Trim() == "0.00")
                {
                    strfcydr = "0";
                    model.FCYDR = 0;
                }
                else
                {
                    model.FCYDR = Decimal.Parse(Decimal.Parse(strfcydr.Trim()).ToString("F2"));
                    //strfcycr = "0";
                    model.FCYCR = 0;
                }
                //
                if (strfcycr.Trim() == "" || strfcycr.Trim() == "0" || strfcycr.Trim() == "0.00")
                {
                    strfcycr = "0";
                    model.FCYCR = 0;
                }
                else
                {
                    model.FCYCR = Decimal.Parse(Decimal.Parse(strfcycr.Trim()).ToString("F2"));
                    //strfcydr = "0";
                    model.FCYDR = 0;
                }
                //
                if ((strfcycr.Trim() != "0" && strfcycr.Trim() != "" && strfcycr.Trim() != "0.00") || (strfcydr.Trim() != "0" && strfcydr.Trim() != "" && strfcydr.Trim() != "0.00"))
                {
                    model.ISFCY = true;
                    model.FCYNO = "USD";
                }
                else
                {
                    model.ISFCY = false;
                    model.FCYNO = "RMB";
                }
                //
                model.ORDNO = this.hdOrdno.Value.Trim();
                //行号
                string stritemno = CwVouchersAddDo.CwVouchersAddDo2.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo2.ToString().Trim();
                model.ITEMNO = int.Parse(stritemno.Trim());
                //客户(系统)
                //model.CORPID = CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim();
                //客户名称
                //model.CUSTOMERNAME = CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim();
                //摘要
                model.EXPLAN = CwVouchersAddDo.CwVouchersAddDo3.Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo3.Trim();
                //科目代码
                model.ACCID = CwVouchersAddDo.CwVouchersAddDo4.Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo4.Trim();
                //科目名称
                model.ACCNAME = CwVouchersAddDo.CwVouchersAddDo5.ToString().Trim().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo5.ToString().Trim();
                //揽货人名称
                model.SALE = CwVouchersAddDo.CwVouchersAddDo13.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo13.ToString().Trim();
                //业务编号
                model.BLNO = CwVouchersAddDo.CwVouchersAddDo14.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo14.ToString().Trim();
                model.ISSYS = true;
                model.MODIFIEDUSER = strUserID;
                //
                //插入明细表
                if (ds.Tables[0].Rows[0]["AR"].ToString().Trim() == model.ACCID.ToString().Trim())//应收
                {
                    if (model.FCYNO.ToString().Trim() == "RMB")
                    {
                        if (ds2 == null)
                        {
                            model.ISDEPTACC = false;//核算部门,按规则设置生成
                            model.ISEMPLACC = false;//核算人员,按规则设置生成
                            model.ISCORPACC = false;//核算客户,按规则设置生成
                            model.ISITEMACC = false;//核算项目,按规则设置生成
                            model.DC = "D";//贷方向
                        }
                        else
                        {
                            model.ISDEPTACC = Boolean.Parse(ds2.Tables[0].Rows[0]["ISDEPTACC"].ToString().Trim());//核算部门,按规则设置生成
                            model.ISEMPLACC = Boolean.Parse(ds2.Tables[0].Rows[0]["ISEMPLACC"].ToString().Trim());//核算人员,按规则设置生成
                            model.ISCORPACC = Boolean.Parse(ds2.Tables[0].Rows[0]["ISCORPACC"].ToString().Trim());//核算客户,按规则设置生成
                            model.ISITEMACC = Boolean.Parse(ds2.Tables[0].Rows[0]["ISITEMACC"].ToString().Trim());//核算项目,按规则设置生成
                            if (ds2.Tables[0].Rows[0]["DC"].ToString().Trim() == "借")
                            {
                                model.DC = "D";//借方向
                            }
                            else
                            {
                                model.DC = "C";//贷方向
                            }
                        }
                    }
                    else if (model.FCYNO.ToString().Trim() == "USD")
                    {
                        if (ds6 == null)
                        {
                            model.ISDEPTACC = false;//核算部门,按规则设置生成
                            model.ISEMPLACC = false;//核算人员,按规则设置生成
                            model.ISCORPACC = false;//核算客户,按规则设置生成
                            model.ISITEMACC = false;//核算项目,按规则设置生成
                            model.DC = "C";//贷方向
                        }
                        else
                        {
                            model.ISDEPTACC = Boolean.Parse(ds6.Tables[0].Rows[0]["ISDEPTACC"].ToString().Trim());//核算部门,按规则设置生成
                            model.ISEMPLACC = Boolean.Parse(ds6.Tables[0].Rows[0]["ISEMPLACC"].ToString().Trim());//核算人员,按规则设置生成
                            model.ISCORPACC = Boolean.Parse(ds6.Tables[0].Rows[0]["ISCORPACC"].ToString().Trim());//核算客户,按规则设置生成
                            model.ISITEMACC = Boolean.Parse(ds6.Tables[0].Rows[0]["ISITEMACC"].ToString().Trim());//核算项目,按规则设置生成
                            if (ds6.Tables[0].Rows[0]["DC"].ToString().Trim() == "借")
                            {
                                model.DC = "D";//借方向
                            }
                            else
                            {
                                model.DC = "C";//贷方向
                            }
                        }
                    }
                    //客户(系统)
                    model.CORPID = CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim();
                    //客户名称
                    model.CUSTOMERNAME = CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim();
                }
                else if (ds.Tables[0].Rows[0]["AP"].ToString().Trim() == model.ACCID.ToString().Trim())//应付
                {
                    if (model.FCYNO.ToString().Trim() == "RMB")
                    {
                        if (ds3 == null)
                        {
                            model.CORPID = "";//客户(系统)
                            model.CUSTOMERNAME = "";//客户(系统)
                            model.ISDEPTACC = false;//核算部门,按规则设置生成
                            model.ISEMPLACC = false;//核算人员,按规则设置生成
                            model.ISCORPACC = false;//核算客户,按规则设置生成
                            model.ISITEMACC = false;//核算项目,按规则设置生成
                            model.DC = "C";//贷方向
                        }
                        else
                        {
                            //客户(系统)
                            model.CORPID = CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim();
                            //客户名称
                            model.CUSTOMERNAME = CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim();
                            model.ISCORPACC = Boolean.Parse(ds3.Tables[0].Rows[0]["ISCORPACC"].ToString().Trim());//核算客户,按规则设置生成
                            model.ISDEPTACC = Boolean.Parse(ds3.Tables[0].Rows[0]["ISDEPTACC"].ToString().Trim());//核算部门,按规则设置生成
                            model.ISEMPLACC = Boolean.Parse(ds3.Tables[0].Rows[0]["ISEMPLACC"].ToString().Trim());//核算人员,按规则设置生成
                            model.ISITEMACC = Boolean.Parse(ds3.Tables[0].Rows[0]["ISITEMACC"].ToString().Trim());//核算项目,按规则设置生成
                            if (ds3.Tables[0].Rows[0]["DC"].ToString().Trim() == "借")
                            {
                                model.DC = "D";//借方向
                            }
                            else
                            {
                                model.DC = "C";//贷方向
                            }
                        }
                    }
                    else if (model.FCYNO.ToString().Trim() == "USD")
                    {
                        if (ds7 == null)
                        {
                            //客户(系统)
                            model.CORPID = "";
                            //客户名称
                            model.CUSTOMERNAME = "";
                            model.ISDEPTACC = false;//核算部门,按规则设置生成
                            model.ISEMPLACC = false;//核算人员,按规则设置生成
                            model.ISCORPACC = false;//核算客户,按规则设置生成
                            model.ISITEMACC = false;//核算项目,按规则设置生成
                            model.DC = "C";//贷方向
                            model.CORPID = "";//客户(系统)
                            model.CUSTOMERNAME = "";//客户(系统)
                        }
                        else
                        {
                            //客户(系统)
                            model.CORPID = CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo12.ToString().Trim();
                            //客户名称
                            model.CUSTOMERNAME = CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim().ToLower().Equals("null") ? "" : CwVouchersAddDo.CwVouchersAddDo11.ToString().Trim();
                            model.ISCORPACC = Boolean.Parse(ds7.Tables[0].Rows[0]["ISCORPACC"].ToString().Trim());//核算客户,按规则设置生成
                            model.ISDEPTACC = Boolean.Parse(ds7.Tables[0].Rows[0]["ISDEPTACC"].ToString().Trim());//核算部门,按规则设置生成
                            model.ISEMPLACC = Boolean.Parse(ds7.Tables[0].Rows[0]["ISEMPLACC"].ToString().Trim());//核算人员,按规则设置生成
                            model.ISITEMACC = Boolean.Parse(ds7.Tables[0].Rows[0]["ISITEMACC"].ToString().Trim());//核算项目,按规则设置生成
                            if (ds7.Tables[0].Rows[0]["DC"].ToString().Trim() == "借")
                            {
                                model.DC = "D";//借方向
                            }
                            else
                            {
                                model.DC = "C";//贷方向
                            }
                        }
                    }
                }
                else
                {
                    //客户(系统)
                    model.CORPID = "";
                    //客户名称
                    model.CUSTOMERNAME = "";
                    model.ISDEPTACC = false;//核算部门,按规则设置生成
                    model.ISEMPLACC = false;//核算人员,按规则设置生成
                    model.ISCORPACC = false;//核算客户,按规则设置生成
                    model.ISITEMACC = false;//核算项目,按规则设置生成
                    model.DC = "C";//贷方向
                    model.CORPID = "";//客户(系统)
                    model.CUSTOMERNAME = "";//客户(系统)
                }
                //
                model.QTYUNIT = "";
                model.QTYDR = 0;
                model.QTYCR = 0;
               // model.ISDEPTACC = false;
                //model.ISEMPLACC = false;
                //model.ISCORPACC = false;
                //model.ISITEMACC = false;
                model.DEPTID = "";
                model.EMPLID = "";
                model.PITEMID = 0;
                model.ITEMID = "0";
                model.FIELD10 = "";
                model.FIELD9 = "";
                model.FIELD8 = "";
                model.FIELD7 = "";
                model.FIELD6 = "";
                model.FIELD5 = "";
                model.FIELD4 = "";
                model.FIELD3 = "";
                model.FIELD2 = "";
                model.FIELD1 = "";
                model.FSETTLCODE = "";
                model.FSETTLENO = "";
                model.PACCID = "";
                model.PROPERTY = 0;
                model.GRADE = 0;
                //model.DC = "";
                //
                CwVouitemsEntityGID = CwVouitemsDA.GetModel(model.GID);
                if (CwVouitemsEntityGID.GID == null)
                {
                    int iResult = CwVouitemsDA.AddAll(model);
                    //为1表示更新成功
                    //为-1有异常,更新失败
                    //为-2更新异常,事务已回滚成功
                    //更新完成后,记录日志
                    if (iResult == 1)
                    {
                        CwVouchersDA CwVouchersDA = new CwVouchersDA();
                        CwVouchersEntity CwVouchersEntity = new CwVouchersEntity();
                        //
                        CwVouchersEntity.ORDNO = this.hdOrdno.Value.Trim();
                        //
                        string strAMTDR = T_ALL_DA.GetStrSQL("AMTDR", "select sum(AMTDR) as AMTDR from cw_vouitems where ORDNO='" + this.hdOrdno.Value.Trim() + "'");
                        string strAMTCR = T_ALL_DA.GetStrSQL("AMTCR", "select sum(AMTCR) as AMTCR from cw_vouitems where ORDNO='" + this.hdOrdno.Value.Trim() + "'");
                        //
                        if ((strAMTDR.Trim() != "0" && strAMTDR.Trim() != "" && strAMTDR.Trim() != "0.00"))
                        {
                            CwVouchersEntity.AMTDR = Decimal.Parse(Decimal.Parse(strAMTDR.Trim()).ToString("F2"));
                        }
                        else
                        {
                            CwVouchersEntity.AMTDR = 0;
                        }
                        //
                        if ((strAMTCR.Trim() != "0" && strAMTCR.Trim() != "" && strAMTCR.Trim() != "0.00"))
                        {
                            CwVouchersEntity.AMTCR = Decimal.Parse(Decimal.Parse(strAMTCR.Trim()).ToString("F2"));
                        }
                        else
                        {
                            CwVouchersEntity.AMTDR = 0;
                        }
                        //
                        iResult = CwVouchersDA.UpdateAMT(CwVouchersEntity);
                        //
                    }
                }
                else
                {
                    alt += model.GID + ",";
                }
            }
            //
            //if (alt != "")
            //{
            //    alt=alt.Trim().Substring(0,alt.Trim().Length-1);
            //    Page.ClientScript.RegisterStartupScript(this.GetType(), "key3", "<script>alert('("+alt+"')数据重复,不允许插入!);</script>");
            //}
        }
        #endregion

        #region 获取账户客户保存的账户信息
        /// <summary>
        /// 获取账户客户保存的账户信息
        /// </summary>
        /// <param name="tempValue"></param>
        /// <returns></returns>
        public JsonCwVouchersAddDoGroupEntity GetPostEntity(string tempValue)
        {
            JsonCwVouchersAddDoGroupEntity CwVouchersAddDoGroupEntity = (JsonCwVouchersAddDoGroupEntity)JSON.Instance.ToObject(tempValue);
            return CwVouchersAddDoGroupEntity;
        }
        #endregion

        protected String getVOUNO(string strY, string strM)
        {
            string sVOUNO = "";
            string strVOUNO = "select top 1 VOUNO from cw_vouchers where ACCYEAR='" + strY + "' and ACCMONTH='" + strM + "' and VOUALLNO<>'" + this.hdVOUALLNO.Value.Trim() + "' 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;
        }
        //
    }
}