using System;
using System.Linq;
using System.Web.Mvc;
using DSWeb.MvcShipping.DAL.MsCwGenlegAccitemsDAL;
using DSWeb.MvcShipping.Models.MsCwGenlegAccitems;
using DSWeb.MvcShipping.Models.MsCwGenlegAccitemsLs;
using DSWeb.MvcShipping.Models.MsCwAccitemsGl;
using DSWeb.MvcShipping.Helper;
using DSWeb.MvcShipping.Comm.Cookie;
using HcUtility.Comm;
using HcUtility.Core;
using System.Collections.Generic;
using DSWeb.MvcShipping.DAL.MsBaseInfoDAL;
using DSWeb.EntityDA;
using DSWeb.Areas.CommMng.DAL;

namespace DSWeb.MvcShipping.Controllers
{
    [JsonRequestBehavior]
    public class MsCwGenlegAccitemsController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Edit()
        {
            return View();
        }

        public ContentResult GetDataList(int start, int limit, string sort, string condition)
        {
            var dataList = MsCwGenlegAccitemsDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), Convert.ToString(Session["COMPANYID"]), sort);
            var list = dataList.Skip(start).Take(limit);
            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = list.ToList() });
            return new ContentResult() { Content = json };
        }

        public ContentResult GetDataListStr(string sort, string condition)
        {
            var dataListStr = MsCwGenlegAccitemsDAL.GetDataListStr(condition, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), Convert.ToString(Session["COMPANYID"]), sort);
            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", data = dataListStr });
            return new ContentResult() { Content = json };
        }  
        
        public ContentResult GetData(string handle, string condition)
        {
            MsCwGenlegAccitems head = null;
            if (handle == "edit")
            {
                head = MsCwGenlegAccitemsDAL.GetData(condition, Convert.ToString(Session["COMPANYID"]), Convert.ToString(Session["USERID"]).Trim());
                head.MODIFIEDUSER = Convert.ToString(Session["USERID"]);
                head.MODIFIEDTIME = DateTime.Now;
            }
            if (head == null)
            {
                head = new MsCwGenlegAccitems();
                head.CORPID = Convert.ToString(Session["COMPANYID"]);
                head.CREATEUSER = Convert.ToString(Session["USERID"]);
                head.CREATETIME = DateTime.Now;
                head.MODIFIEDUSER = Convert.ToString(Session["USERID"]);
                head.MODIFIEDTIME = DateTime.Now;
            }
            var json = JsonConvert.Serialize(
               new { Success = true, Message = "查询成功", data = head });
            return new ContentResult() { Content = json };
        }
        
        public ContentResult Save(string opstatus, string data)
        {
            if (Convert.ToString(Session["COMPANYID"]).Trim() == "" || Convert.ToString(Session["USERID"]).Trim() == "" || Convert.ToString(Session["CODENAME"]).Trim() == "" || Convert.ToString(Session["SHOWNAME"]).Trim() == "" || Convert.ToString(Session["DEPTNAME"]).Trim() == "")
            {
                var jsonRespose2 = new JsonResponse { Success = false, Message = "登录超时,请退出系统重新登录!" };
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose2) }; 
            }
            //
            var headData = JsonConvert.Deserialize<MsCwGenlegAccitems>(data);
            var isPost = true;
            #region 默认值
            T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
            string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(Convert.ToString(Session["USERID"]));
            string LINKGID = T_ALL_DA.GetStrSQL("GID", "select top 1 GID from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ACCNAME='" + headData.LINKNAME.ToString().Trim() + "'");
            headData.LINKGID = LINKGID;
            headData.STARTGID = BasicDataRefDAL.GetCwSTARTGID(Convert.ToString(Session["USERID"]));
            headData.IsInitialEntry = true;
            if (headData.CORPID == "" || headData.CORPID == null)
            {
                headData.CORPID = Convert.ToString(Session["COMPANYID"]);
            }
            if (headData.CREATEUSER == "" || headData.CREATEUSER == null)
            {
                headData.CREATEUSER = Convert.ToString(Session["CREATEUSER"]);
                headData.CREATETIME = DateTime.Now;
            }
            headData.MODIFIEDUSER = Convert.ToString(Session["USERID"]);
            headData.MODIFIEDTIME = DateTime.Now;            
            #endregion
            //
            if (opstatus == "add")
            {
                headData.DbOperationType = DbOperationType.DbotIns;                
            }
            else if (opstatus == "edit")
            {
                headData.DbOperationType = DbOperationType.DbotUpd;
                headData.ModelUIStatus = "E";
            }
            else
            {
                headData.DbOperationType = DbOperationType.DbotDel;
            }

            if (isPost)
            {
                var modb = new ModelObjectDB();
                DBResult result = modb.Save(headData);
                if (result.Message.ToString().IndexOf("重复键") > -1)
                {
                    var jsonRespose = new JsonResponse { Success = false, Message = "数据重复,不允许保存!" };
                    return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
                }
                else
                {
                    var GID = headData.GID;
                    var jsonRespose = new JsonResponse
                    {
                        Success = result.Success,
                        Message = result.Message,
                        Data = MsCwGenlegAccitemsDAL.GetData("GID='" + GID + "'", Convert.ToString(Session["COMPANYID"]), Convert.ToString(Session["USERID"]).Trim())
                    };
                    return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
                }
            }
            else {
                var jsonRespose = new JsonResponse { Success = false, Message = "数据重复,不允许保存!" };
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };      
            }
        }

        #region 期初列表操作
        public ContentResult GetDataListCA(string PACCGID, string condition)
        {
            if (PACCGID.Trim().IndexOf("root") > -1)
            {
                PACCGID = "ZC','FZ','GT','QY','CB','SY";
            }
            if (!string.IsNullOrEmpty(PACCGID))
            {
                condition += " and PACCGID in ('" + PACCGID + "')";
            }
            var dataList = MsCwGenlegAccitemsDAL.GetDataListCA(condition, Convert.ToString(Session["USERID"]));
            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
            return new ContentResult() { Content = json };
        }

        public ContentResult SaveRow(string data)
        {
            if (Convert.ToString(Session["COMPANYID"]).Trim() == "" || Convert.ToString(Session["USERID"]).Trim() == "" || Convert.ToString(Session["CODENAME"]).Trim() == "" || Convert.ToString(Session["SHOWNAME"]).Trim() == "" || Convert.ToString(Session["DEPTNAME"]).Trim() == "")
            {
                var jsonRespose2 = new JsonResponse { Success = false, Message = "登录超时,请退出系统重新登录!" };
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose2) };
            }
            //
            var isPost = true;
            var errorstr = "";
            T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
            MsCwGenlegAccitems headRow = new MsCwGenlegAccitems();
            var headData = JsonConvert.Deserialize<MsCwAccitemsGl>(data);
            //
            string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(Convert.ToString(Session["USERID"]).Trim());
            string strCwSTARTMONTH = BasicDataRefDAL.GetCwSTARTMONTH(Convert.ToString(Session["USERID"]).Trim());
            string strCwACCDATE = BasicDataRefDAL.GetCwACCDATE(Convert.ToString(Session["USERID"]).Trim());
            string upGid = "select top 1 isnull(gid,'') as gid from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and LINKGID='" + headData.GID.ToString().Trim() + "' and DEPTACC='' and EMPLACC='' and CORPACC='' and ITEMACC='' and CURRENCY='RMB'";
            if (strCwSTARTMONTH.Substring(0,4) == strCwACCDATE.Substring(0,4))
            {
                upGid += " and ACCDATE='" + strCwSTARTMONTH + "'";
            }
            else
            {
                upGid += " and ACCDATE=(SUBSTRING('" + strCwACCDATE + "',1,4)+'-01')";
            }
            upGid = T_ALL_DA.GetStrSQL("gid", upGid);
            //
            #region 默认值
            headRow.YEAR = DateTime.Parse(strCwACCDATE + "-01").Year.ToString();//年
            headRow.MONTH = DateTime.Parse(strCwACCDATE + "-01").Month.ToString();//月
            if (DateTime.Parse(strCwACCDATE + "-01").Month.ToString().Length == 1)
            {
                headRow.MONTH = "0" + DateTime.Parse(strCwACCDATE + "-01").Month.ToString();//月
            }
            headRow.LINKGID = headData.GID;//科目GID
            headRow.DEPTACC = "";//核算部门
            headRow.EMPLACC = "";//核算人员
            headRow.CORPACC = "";//核算客户
            headRow.ITEMACC = "";//核算项目
            headRow.CURRENCY = "RMB";//币别
            headRow.AMTYEARDR = headData.DR;//RMB期初借方
            headRow.AMTYEARCR = headData.CR;//RMB期初贷方
            //headRow.AMTYEARBLC = headRow.AMTYEARDR - headRow.AMTYEARCR;//RMB期初余额
            headRow.AMTDR = 0;//RMB本月借方
            headRow.AMTCR = 0;//RMB本月贷方
            headRow.AMTBLC = 0;//RMB本月余额
            headRow.AMTLASTDR = headData.DR;//RMB累计借方
            headRow.AMTLASTCR = headData.CR;//RMB累计贷方
            //headRow.AMTLASTBLC = headRow.AMTLASTDR - headRow.AMTLASTCR;//RMB累计余额
            headRow.FCYYEARDR = 0;//外币期初借方
            headRow.FCYYEARCR = 0;//外币期初贷方
            headRow.FCYYEARBLC = 0;//外币期初余额
            headRow.FCYDR = 0;//外币本月借方
            headRow.FCYCR = 0;//外币本月贷方
            headRow.FCYBLC = 0;//外币本月余额
            headRow.FCYLASTDR = 0;//外币累计借方
            headRow.FCYLASTCR = 0;//外币累计贷方
            headRow.FCYLASTBLC = 0;//外币累计余额
            headRow.QTYYEARDR = headData.DR;//期初借方
            headRow.QTYYEARCR = headData.CR;//期初贷方
            //headRow.QTYYEARBLC = headRow.QTYYEARDR - headRow.QTYYEARCR;//期初余额
            headRow.QTYDR = 0;//本月借方
            headRow.QTYCR = 0;//本月贷方
            headRow.QTYBLC = 0;//本月余额
            headRow.QTYLASTDR = headData.DR;//累计借方
            headRow.QTYLASTCR = headData.CR;//累计贷方
            //headRow.QTYLASTBLC = headRow.QTYLASTDR - headRow.QTYLASTCR;//累计余额
            headRow.PFADR = headData.PFADR;//损益类科目本年累计数借方
            headRow.PFACR = headData.PFACR;//损益类科目本年累计数贷方

            if (headData.DC.ToString().Trim() == "借")
            {
                headRow.AMTYEARBLC = headRow.AMTYEARDR - headRow.AMTYEARCR;//RMB期初余额
                headRow.AMTLASTBLC = headRow.AMTLASTDR - headRow.AMTLASTCR;//RMB累计余额
                headRow.QTYYEARBLC = headRow.QTYYEARDR - headRow.QTYYEARCR;//期初余额
                headRow.QTYLASTBLC = headRow.QTYLASTDR - headRow.QTYLASTCR;//累计余额
            }
            else
            {
                headRow.AMTYEARBLC = headRow.AMTYEARCR - headRow.AMTYEARDR;//RMB期初余额
                headRow.AMTLASTBLC = headRow.AMTLASTCR - headRow.AMTLASTDR;//RMB累计余额
                headRow.QTYYEARBLC = headRow.QTYYEARCR - headRow.QTYYEARDR;//期初余额
                headRow.QTYLASTBLC = headRow.QTYLASTCR - headRow.QTYLASTDR;//累计余额
            }
            
            headRow.CORPID = Convert.ToString(Session["COMPANYID"]);//分公司代码
            headRow.ISDELETE = false;//是否删除
            headRow.DELETEUSER = Convert.ToString(Session["USERID"]);//删除人
            headRow.DELETETIME = DateTime.Now;//删除时间
            headRow.CREATEUSER = Convert.ToString(Session["USERID"]);//创建人gid
            headRow.CREATETIME = DateTime.Now;//创建时间
            headRow.MODIFIEDUSER = Convert.ToString(Session["USERID"]);//更改操作人gid
            headRow.MODIFIEDTIME = DateTime.Now;//更改操作时间
            headRow.STARTGID = BasicDataRefDAL.GetCwSTARTGID(Convert.ToString(Session["USERID"]));
            headRow.ACCDATE = strCwACCDATE;
            headRow.IsInitialEntry = true;
            //
            if (upGid.Trim() == "")//"add"
            {
                headRow.GID = Guid.NewGuid().ToString();//唯一编码
                headRow.DbOperationType = DbOperationType.DbotIns;
            }
            else//"edit"
            {
                headRow.GID = upGid.Trim();//唯一编码
                headRow.DbOperationType = DbOperationType.DbotUpd;
                headRow.ModelUIStatus = "E";
            }
            #endregion
            //
            if (isPost)
            {
                var modb = new ModelObjectDB();
                DBResult result = modb.Save(headRow);
                if (result.Message.ToString().IndexOf("重复键") > -1)
                {
                    var jsonRespose = new JsonResponse { Success = false, Message = "数据重复,不允许保存!" };
                    return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
                }
                else
                {
                    MsCwGenlegAccitemsDAL.SaveRow(headData, Convert.ToString(Session["USERID"]), Convert.ToString(Session["COMPANYID"]), "RMB");
                    //
                    var GID = headRow.GID;
                    var jsonRespose = new JsonResponse
                    {
                        Success = result.Success,
                        Message = result.Message,
                        Data = MsCwGenlegAccitemsDAL.GetData("GID='" + GID + "'", Convert.ToString(Session["COMPANYID"]), Convert.ToString(Session["USERID"]).Trim())
                    };
                    return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
                }
            }
            else
            {
                var jsonRespose = new JsonResponse { Success = false, Message = errorstr + "重复,不允许保存!" };
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
            }
        }

        public ContentResult GetDataListHJ(string condition)
        {
            var dataList = MsCwGenlegAccitemsDAL.GetDataListHJ(Convert.ToString(Session["USERID"]));
            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
            return new ContentResult() { Content = json };
        }

        public ContentResult GetDataEdit(string condition)
        {
            var dataList = MsCwGenlegAccitemsDAL.GetDataEdit(condition, Convert.ToString(Session["COMPANYID"]), Convert.ToString(Session["USERID"]).Trim());
            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
            return new ContentResult() { Content = json };
        }

        public ContentResult SaveEdit(string data, string body)
        {
            if (Convert.ToString(Session["COMPANYID"]).Trim() == "" || Convert.ToString(Session["USERID"]).Trim() == "" || Convert.ToString(Session["CODENAME"]).Trim() == "" || Convert.ToString(Session["SHOWNAME"]).Trim() == "" || Convert.ToString(Session["DEPTNAME"]).Trim() == "")
            {
                var jsonRespose2 = new JsonResponse { Success = false, Message = "登录超时,请退出系统重新登录!" };
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose2) };
            }
            //
            var isPost = true;
            var errorstr = "";
            T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
            var headData = JsonConvert.Deserialize<MsCwAccitemsGl>(data);
            var bodyList = JsonConvert.Deserialize<List<MsCwGenlegAccitemsLs>>(body);
            string strCwACCDATE = BasicDataRefDAL.GetCwACCDATE(Convert.ToString(Session["USERID"]));
            string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(Convert.ToString(Session["USERID"]));
            //
            if (isPost)
            {
                var modb = new ModelObjectDB();
                DBResult result = null;
                if (bodyList != null)
                {
                    foreach (var enumValue in bodyList)
                    {
                        #region 默认值
                        string CURRENCY = enumValue.CURRENCY.ToString().Trim() == "" ? "RMB" : enumValue.CURRENCY.ToString().Trim();
                        MsCwGenlegAccitems headRow = new MsCwGenlegAccitems();
                        headRow.YEAR = DateTime.Parse(strCwACCDATE + "-01").Year.ToString();//年
                        headRow.MONTH = DateTime.Parse(strCwACCDATE + "-01").Month.ToString();//月
                        if (DateTime.Parse(strCwACCDATE + "-01").Month.ToString().Length == 1)
                        {
                            headRow.MONTH = "0" + DateTime.Parse(strCwACCDATE + "-01").Month.ToString();//月
                        }
                        headRow.LINKGID = headData.GID;//科目GID
                        headRow.DEPTACC = enumValue.DEPTACC.ToString().Trim();//核算部门
                        headRow.EMPLACC = enumValue.EMPLACC.ToString().Trim();//核算人员
                        headRow.CORPACC = enumValue.CORPACC.ToString().Trim();//核算客户
                        headRow.ITEMACC = enumValue.ITEMACC.ToString().Trim();//核算项目
                        headRow.CURRENCY = CURRENCY.Trim();//币别
                        //
                        headRow.AMTYEARDR = Decimal.Parse(enumValue.DR);//RMB期初借方
                        headRow.AMTYEARCR = Decimal.Parse(enumValue.CR);//RMB期初贷方
                        //headRow.AMTYEARBLC = headRow.AMTYEARDR - headRow.AMTYEARCR;//RMB期初余额
                        headRow.AMTDR = 0;//RMB本月借方
                        headRow.AMTCR = 0;//RMB本月贷方
                        headRow.AMTBLC = 0;//RMB本月余额
                        headRow.AMTLASTDR = Decimal.Parse(enumValue.DR);//RMB累计借方
                        headRow.AMTLASTCR = Decimal.Parse(enumValue.CR);//RMB累计贷方
                        //headRow.AMTLASTBLC = headRow.AMTLASTDR - headRow.AMTLASTCR;//RMB累计余额

                        if (headData.DC.ToString().Trim() == "借")
                        {
                            headRow.AMTYEARBLC = headRow.AMTYEARDR - headRow.AMTYEARCR;//RMB期初余额
                            headRow.AMTLASTBLC = headRow.AMTLASTDR - headRow.AMTLASTCR;//RMB累计余额
                        }
                        else
                        {
                            headRow.AMTYEARBLC = headRow.AMTYEARCR - headRow.AMTYEARDR;//RMB期初余额
                            headRow.AMTLASTBLC = headRow.AMTLASTCR - headRow.AMTLASTDR;//RMB累计余额
                        }

                        headRow.FCYYEARDR = 0;//外币期初借方
                        headRow.FCYYEARCR = 0;//外币期初贷方
                        headRow.FCYYEARBLC = 0;//外币期初余额
                        headRow.FCYDR = 0;//外币本月借方
                        headRow.FCYCR = 0;//外币本月贷方
                        headRow.FCYBLC = 0;//外币本月余额
                        headRow.FCYLASTDR = 0;//外币累计借方
                        headRow.FCYLASTCR = 0;//外币累计贷方
                        headRow.FCYLASTBLC = 0;//外币累计余额

                        Decimal rate = 1;
                        if (CURRENCY.Trim() != "RMB")
                        {
                            headRow.AMTYEARDR = 0;//RMB期初借方
                            headRow.AMTYEARCR = 0;//RMB期初贷方
                            headRow.AMTYEARBLC = 0;//RMB期初余额
                            headRow.AMTDR = 0;//RMB本月借方
                            headRow.AMTCR = 0;//RMB本月贷方
                            headRow.AMTBLC = 0;//RMB本月余额
                            headRow.AMTLASTDR = 0;//RMB累计借方
                            headRow.AMTLASTCR = 0;//RMB累计贷方
                            headRow.AMTLASTBLC = 0;//RMB累计余额

                            headRow.FCYYEARDR = Decimal.Parse(enumValue.DR);//外币期初借方
                            headRow.FCYYEARCR = Decimal.Parse(enumValue.CR);//外币期初贷方
                            //headRow.FCYYEARBLC = headRow.FCYYEARDR - headRow.FCYYEARCR;//外币期初余额
                            headRow.FCYDR = 0;//外币本月借方
                            headRow.FCYCR = 0;//外币本月贷方
                            headRow.FCYBLC = 0;//外币本月余额
                            headRow.FCYLASTDR = Decimal.Parse(enumValue.DR);//外币累计借方
                            headRow.FCYLASTCR = Decimal.Parse(enumValue.CR);//外币累计贷方
                            //headRow.FCYLASTBLC = headRow.FCYLASTDR - headRow.FCYLASTCR;//外币累计余额
                            
                            if (headData.DC.ToString().Trim() == "借")
                            {
                                headRow.FCYYEARBLC = headRow.FCYYEARDR - headRow.FCYYEARCR;//外币期初余额
                                headRow.FCYLASTBLC = headRow.FCYLASTDR - headRow.FCYLASTCR;//外币累计余额
                            }
                            else
                            {
                                headRow.FCYYEARBLC = headRow.FCYYEARCR - headRow.FCYYEARDR;//外币期初余额
                                headRow.FCYLASTBLC = headRow.FCYLASTCR - headRow.FCYLASTDR;//外币累计余额
                            }

                            //rate = Decimal.Parse(T_ALL_DA.GetStrSQL("DEFAULTRATE", "select top 1 isnull(DEFAULTRATE,1) as DEFAULTRATE from code_currency where CODENAME='" + CURRENCY.Trim() + "'"));
                            rate = Decimal.Parse(T_ALL_DA.GetStrSQL("DEFAULTRATE", "SELECT top 1 DEFAULTRATE=isnull((select top 1 BEGRATE from cw_currency_rate where LINKGID=code_currency.GID and ISDELETE=0 and ACCDATE= '" + strCwACCDATE + "' and STARTGID='" + strCwSTARTGID + "'),DEFAULTRATE) from code_currency WITH(NOLOCK) where CODENAME='" + CURRENCY.Trim() + "'"));// and CORPID='" + Convert.ToString(Session["COMPANYID"]) + "'
                        }

                        headRow.QTYYEARDR = Decimal.Parse(enumValue.DR) * rate;//期初借方
                        headRow.QTYYEARCR = Decimal.Parse(enumValue.CR) * rate;//期初贷方
                        //headRow.QTYYEARBLC = headRow.QTYYEARDR - headRow.QTYYEARCR;//期初余额
                        headRow.QTYDR = 0;//本月借方
                        headRow.QTYCR = 0;//本月贷方
                        headRow.QTYBLC = 0;//本月余额
                        headRow.QTYLASTDR = Decimal.Parse(enumValue.DR) * rate;//累计借方
                        headRow.QTYLASTCR = Decimal.Parse(enumValue.CR) * rate;//累计贷方
                        //headRow.QTYLASTBLC = headRow.QTYLASTDR - headRow.QTYLASTCR;//累计余额

                        if (headData.DC.ToString().Trim() == "借")
                        {
                            headRow.QTYYEARBLC = headRow.QTYYEARDR - headRow.QTYYEARCR;//期初余额
                            headRow.QTYLASTBLC = headRow.QTYLASTDR - headRow.QTYLASTCR;//累计余额
                        }
                        else
                        {
                            headRow.QTYYEARBLC = headRow.QTYYEARCR - headRow.QTYYEARDR;//期初余额
                            headRow.QTYLASTBLC = headRow.QTYLASTCR - headRow.QTYLASTDR;//累计余额
                        }

                        headRow.PFADR = enumValue.PFADR;//损益类科目本年累计数借方
                        headRow.PFACR = enumValue.PFACR;//损益类科目本年累计数贷方

                        headRow.CORPID = Convert.ToString(Session["COMPANYID"]);//分公司代码
                        headRow.ISDELETE = false;//是否删除
                        headRow.DELETEUSER = Convert.ToString(Session["USERID"]);//删除人
                        headRow.DELETETIME = DateTime.Now;//删除时间
                        headRow.CREATEUSER = Convert.ToString(Session["USERID"]);//创建人gid
                        headRow.CREATETIME = DateTime.Now;//创建时间
                        headRow.MODIFIEDUSER = Convert.ToString(Session["USERID"]);//更改操作人gid
                        headRow.MODIFIEDTIME = DateTime.Now;//更改操作时间
                        headRow.STARTGID = BasicDataRefDAL.GetCwSTARTGID(Convert.ToString(Session["USERID"]));
                        headRow.ACCDATE = strCwACCDATE;//更改操作时间
                        headRow.IsInitialEntry = true;
                        //
                        if (enumValue.GID.ToString().Trim() == "*")//upGid.Trim() == ""//"add"
                        {
                            headRow.GID = Guid.NewGuid().ToString();//唯一编码
                            headRow.DbOperationType = DbOperationType.DbotIns;
                        }
                        else//"edit"
                        {
                            headRow.GID = enumValue.GID.ToString().Trim();//唯一编码
                            headRow.DbOperationType = DbOperationType.DbotUpd;
                            headRow.ModelUIStatus = "E";
                            //
                            if (enumValue.ISENABLE)
                            {
                                continue;
                            }
                        }
                        #endregion
                        //
                        modb = new ModelObjectDB();
                        result = modb.Save(headRow);
                        if (result.Message.ToString().IndexOf("重复键") > -1)
                        {
                            //errorstr += "数据重复,不允许保存!";
                            //var jsonRespose = new JsonResponse { Success = false, Message = "重复,不允许保存!" };
                            //return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };

                            string strLs = "select top 1 gid from cw_genleg_accitems where [YEAR]='" + headRow.YEAR + "' and [MONTH]='" + headRow.MONTH + "' and [LINKGID]='" + headRow.LINKGID + "' and [DEPTACC]='" + headRow.DEPTACC + "' and [EMPLACC]='" + headRow.EMPLACC + "' and [CORPACC]='" + headRow.CORPACC + "' and [ITEMACC]='" + headRow.ITEMACC + "' and [CURRENCY]='" + headRow.CURRENCY + "' and [STARTGID]='" + headRow.STARTGID + "'";
                            string sGID = T_ALL_DA.GetStrSQL("gid", strLs);

                            headRow.GID = sGID;//唯一编码
                            headRow.DbOperationType = DbOperationType.DbotUpd;
                            headRow.ModelUIStatus = "E";
                            result = modb.Save(headRow);
                        }
                        //
                        headData.DR = Decimal.Parse(enumValue.DR);
                        headData.CR = Decimal.Parse(enumValue.CR);
                        MsCwGenlegAccitemsDAL.SaveRow(headData, Convert.ToString(Session["USERID"]), Convert.ToString(Session["COMPANYID"]), CURRENCY);
                    }
                }
                //
                var LINKGID = headData.GID.ToString().Trim();
                if (result.Message.ToString().IndexOf("重复键") > -1)
                {
                    result.Message = "数据重复,不允许保存!";
                }
                var jsonRespose = new JsonResponse
                {
                    Success = result.Success,
                    Message = result.Message,
                    Data = MsCwGenlegAccitemsDAL.GetData("LINKGID='" + LINKGID + "'", Convert.ToString(Session["COMPANYID"]), Convert.ToString(Session["USERID"]).Trim())
                };
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };

            }
            else
            {
                var jsonRespose = new JsonResponse { Success = false, Message = errorstr + "重复,不允许保存!" };
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
            }
        }

        public ContentResult DeleteDetail(string dataER, string data)
        {
            if (Convert.ToString(Session["COMPANYID"]).Trim() == "" || Convert.ToString(Session["USERID"]).Trim() == "" || Convert.ToString(Session["CODENAME"]).Trim() == "" || Convert.ToString(Session["SHOWNAME"]).Trim() == "" || Convert.ToString(Session["DEPTNAME"]).Trim() == "")
            {
                var jsonRespose2 = new JsonResponse { Success = false, Message = "登录超时,请退出系统重新登录!" };
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose2) };
            }
            //
            var modb = new ModelObjectDB();
            var headData = JsonConvert.Deserialize<MsCwAccitemsGl>(dataER);
            var head = JsonConvert.Deserialize<MsCwGenlegAccitems>(data);
            DBResult result = MsCwGenlegAccitemsDAL.DeleteDetail(headData, head, Convert.ToString(Session["USERID"]), Convert.ToString(Session["COMPANYID"]));
            var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
            return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
        }

        public ContentResult GetISENABLE(string ISENABLE)
        {
            int icount = MsCwGenlegAccitemsDAL.GetISENABLE(Convert.ToString(Session["USERID"]).Trim());
            DBResult result = new DBResult();
            if (icount == 0)
            {
                result.Success = false;
                result.Message = "数据已入账,不予以操作!";
            }
            else
            {
                result.Success = true;
                result.Message = "操作完成";
            }
            var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
            return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
        }

        public ContentResult setISENABLE(string ISENABLE)
        {
            if (Convert.ToString(Session["COMPANYID"]).Trim() == "" || Convert.ToString(Session["USERID"]).Trim() == "" || Convert.ToString(Session["CODENAME"]).Trim() == "" || Convert.ToString(Session["SHOWNAME"]).Trim() == "" || Convert.ToString(Session["DEPTNAME"]).Trim() == "")
            {
                var jsonRespose2 = new JsonResponse { Success = false, Message = "登录超时,请退出系统重新登录!" };
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose2) };
            }
            //
            DBResult result = MsCwGenlegAccitemsDAL.setISENABLE(ISENABLE, Convert.ToString(Session["USERID"]), Convert.ToString(Session["COMPANYID"]));
            var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
            return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
        }

        public ContentResult setSTARTMONTH(string isSTART, string STARTMONTH)
        {
            if (Convert.ToString(Session["COMPANYID"]).Trim() == "" || Convert.ToString(Session["USERID"]).Trim() == "" || Convert.ToString(Session["CODENAME"]).Trim() == "" || Convert.ToString(Session["SHOWNAME"]).Trim() == "" || Convert.ToString(Session["DEPTNAME"]).Trim() == "")
            {
                var jsonRespose2 = new JsonResponse { Success = false, Message = "登录超时,请退出系统重新登录!" };
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose2) };
            }
            //
            DBResult result = MsCwGenlegAccitemsDAL.setSTARTMONTH(isSTART,STARTMONTH, Convert.ToString(Session["USERID"]), Convert.ToString(Session["COMPANYID"]));
            var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
            return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
        }

        #region 启用的账套
        public ContentResult GetSTARTMONTH(string STARTGID)
        {
            var strList = MsCwGenlegAccitemsDAL.GetSTARTMONTH(STARTGID, Convert.ToString(Session["COMPANYID"]), Convert.ToString(Session["USERID"]));
            if (strList.Trim() == "-1")
            {
                var json = JsonConvert.Serialize(new { Success = false, Message = "", data = "" });
                return new ContentResult() { Content = json };
            }
            else
            {
                var json = JsonConvert.Serialize(new { Success = true, Message = "操作成功!", data = strList });
                return new ContentResult() { Content = json };
            }
        }
        #endregion     
        #endregion
    }
}