private DXInfo.Models.tbStockMain AddStockMain(string cnvcWhCode, string cnvcDeptId, string strOperId, string strOperName,
                                                       int cnnOperType, DateTime cndBusinessDate)
        {
            DXInfo.Models.tbStockMain tbStockMain = new DXInfo.Models.tbStockMain();

            tbStockMain.cnvcWhCode  = cnvcWhCode;
            tbStockMain.cnvcDeptId  = cnvcDeptId;
            tbStockMain.cnnOperType = cnnOperType;
            tbStockMain.cnnSource   = 1;

            tbStockMain.cndCreateDate   = DateTime.Now;
            tbStockMain.cnvcCreaterId   = strOperId;
            tbStockMain.cnvcCreaterName = strOperName;

            tbStockMain.cndCheckDate    = tbStockMain.cndCreateDate;
            tbStockMain.cnvcCheckerId   = tbStockMain.cnvcCreaterId;
            tbStockMain.cnvcCheckerName = tbStockMain.cnvcCreaterName;

            tbStockMain.cndBusinessDate = cndBusinessDate;
            tbStockMain.cnnYear         = cndBusinessDate.Year;
            tbStockMain.cnnMonth        = cndBusinessDate.Month;
            tbStockMain.cnnStatus       = (int)StockStatus.Check;
            tbStockMain.cnvcComments    = "月结自动生成下月期初库存";

            Uow.tbStockMain.Add(tbStockMain);

            Uow.Commit();

            DXInfo.Models.tbStockMainLog tbStockMainLog = new DXInfo.Models.tbStockMainLog();
            ServiceHelper.SetEntity <DXInfo.Models.tbStockMain, DXInfo.Models.tbStockMainLog>(tbStockMain, tbStockMainLog);
            Uow.tbStockMainLog.Add(tbStockMainLog);
            return(tbStockMain);
        }
        private string cancelBalance(HttpContext context)
        {
            try
            {
                //using (DXInfo.Models.AMSCM amscm = new DXInfo.Models.AMSCM())
                //{
                using (TransactionScope transaction = new TransactionScope())
                {
                    int icnnYear  = Convert.ToInt32(context.Request.Form["cnnYear"]);
                    int icnnMonth = Convert.ToInt32(context.Request.Form["cnnMonth"]);
                    int icount    = (from d in Uow.tbMonthlyBalance.GetAll()
                                     where d.cnnYear >= icnnYear && d.cnnMonth > icnnMonth && d.cnbIsBalance
                                     select d).Count();
                    if (icount > 0)
                    {
                        return(ServiceHelper.JsonSerializer <JEasyUIResult>(new JEasyUIResult(false, "请先取消以前月份月结!")));
                    }
                    DXInfo.Models.tbMonthlyBalance tbMonthlyBalance = Uow.tbMonthlyBalance.GetById(g => g.cnnYear == icnnYear && g.cnnMonth == icnnMonth);
                    if (!tbMonthlyBalance.cnbIsBalance)
                    {
                        return(ServiceHelper.JsonSerializer <JEasyUIResult>(new JEasyUIResult(false, "未月结!")));
                    }

                    tbMonthlyBalance.cnbIsBalance = false;

                    tbMonthlyBalance.cndModifyDate = DateTime.Now;
                    tbMonthlyBalance.cnvcModifier  = context.User.Identity.Name;
                    CMSMStruct.LoginStruct ls1 = (CMSMStruct.LoginStruct)context.Session["Login"];
                    tbMonthlyBalance.cnvcModifierName = ls1.strOperName;

                    tbMonthlyBalance.cndBalanceDate   = null;
                    tbMonthlyBalance.cnvcBalancer     = string.Empty;
                    tbMonthlyBalance.cnvcBalancerName = string.Empty;

                    Uow.Commit();
                    DXInfo.Models.tbMonthlyBalanceLog tbMonthlyBalanceLog = new DXInfo.Models.tbMonthlyBalanceLog();
                    ServiceHelper.SetEntity <DXInfo.Models.tbMonthlyBalance, DXInfo.Models.tbMonthlyBalanceLog>(tbMonthlyBalance, tbMonthlyBalanceLog);
                    Uow.tbMonthlyBalanceLog.Add(tbMonthlyBalanceLog);

                    int         icnnCheckStatus = (int)StockStatus.Check;
                    List <long> lcnnDetailId    = (from main in Uow.tbStockMain.GetAll()
                                                   join d in Uow.tbStockDetail.GetAll() on main.cnnMainId equals d.cnnMainId into md
                                                   from detail in md.DefaultIfEmpty()
                                                   where main.cnnYear == tbMonthlyBalance.cnnYear && main.cnnMonth == tbMonthlyBalance.cnnMonth &&
                                                   main.cnnStatus == icnnCheckStatus && main.cnnOperType != 0 && main.cnnOperType != 1
                                                   orderby main.cnnMainId
                                                   select detail.cnnDetailId).Distinct().ToList();
                    foreach (long cnnDetailId in lcnnDetailId)
                    {
                        DXInfo.Models.tbStockDetail tbStockDetail = Uow.tbStockDetail.GetById(g => g.cnnDetailId == cnnDetailId);
                        tbStockDetail.cnnPrice  = 0;
                        tbStockDetail.cnnAmount = 0;

                        DXInfo.Models.tbStockDetailLog tbStockDetailLog = new DXInfo.Models.tbStockDetailLog();
                        ServiceHelper.SetEntity <DXInfo.Models.tbStockDetail, DXInfo.Models.tbStockDetailLog>(tbStockDetail, tbStockDetailLog);
                        Uow.tbStockDetailLog.Add(tbStockDetailLog);
                    }
                    int inextcnnYear  = icnnYear;
                    int inextcnnMonth = icnnMonth + 1;
                    if (icnnMonth == 12)
                    {
                        inextcnnYear += 1;
                        inextcnnMonth = 1;
                    }
                    var q = (from main in Uow.tbStockMain.GetAll()
                             join d in Uow.tbStockDetail.GetAll() on main.cnnMainId equals d.cnnMainId into md
                             from detail in md.DefaultIfEmpty()
                             where main.cnnYear == inextcnnYear && main.cnnMonth == inextcnnMonth &&
                             main.cnnStatus == icnnCheckStatus && main.cnnSource == 1
                             orderby main.cnnMainId
                             select new { detail.cnnMainId, detail.cnnDetailId }).Distinct().ToList();
                    var q1 = (from d in q select d.cnnMainId).Distinct().ToList();
                    var q2 = (from d in q select d.cnnDetailId).Distinct().ToList();
                    if (q1.Count > 0)
                    {
                        foreach (long cnnMainId in q1)
                        {
                            DXInfo.Models.tbStockMain tbStockMain = Uow.tbStockMain.GetById(g => g.cnnMainId == cnnMainId);
                            tbStockMain.cnnStatus = (int)StockStatus.Delete;
                            Uow.tbStockMain.Delete(tbStockMain);

                            DXInfo.Models.tbStockMainLog tbStockMainLog = new DXInfo.Models.tbStockMainLog();
                            ServiceHelper.SetEntity <DXInfo.Models.tbStockMain, DXInfo.Models.tbStockMainLog>(tbStockMain, tbStockMainLog);
                            Uow.tbStockMainLog.Add(tbStockMainLog);
                        }
                    }
                    if (q2.Count > 0)
                    {
                        foreach (long cnnDetailId in q2)
                        {
                            DXInfo.Models.tbStockDetail tbStockDetail = Uow.tbStockDetail.GetById(g => g.cnnDetailId == cnnDetailId);
                            Uow.tbStockDetail.Delete(tbStockDetail);

                            DXInfo.Models.tbStockDetailLog tbStockDetailLog = new DXInfo.Models.tbStockDetailLog();
                            ServiceHelper.SetEntity <DXInfo.Models.tbStockDetail, DXInfo.Models.tbStockDetailLog>(tbStockDetail, tbStockDetailLog);
                            Uow.tbStockDetailLog.Add(tbStockDetailLog);
                        }
                    }
                    Uow.Commit();
                    transaction.Complete();
                }
                //}
            }
            catch (DbUpdateException dex)
            {
                ExceptionPolicy.HandleException(dex, ServiceHelper.ExceptionPolicy);
                return(ServiceHelper.JsonSerializer <JEasyUIResult>(new JEasyUIResult(false, dex.Message)));
            }
            return(ServiceHelper.JsonSerializer <JEasyUIResult>(new JEasyUIResult(true, "")));
        }