Пример #1
0
        public static void AddStockDetal(AMSCM.Models.AMSCM amscm, AMSCM.Models.tbStockMain tbStockMain, string cnvcInvCode
                                         , string cnvcComUnitCode, decimal cnnQuantity, string cnvcMainComUnitCode, decimal cnnMainQuantity, decimal cnnPrice, decimal cnnAmount)
        {
            AMSCM.Models.tbStockDetail tbStockDetail = new AMSCM.Models.tbStockDetail();
            tbStockDetail.cnnMainId           = tbStockMain.cnnMainId;
            tbStockDetail.cnvcInvCode         = cnvcInvCode;
            tbStockDetail.cnvcComUnitCode     = cnvcComUnitCode;
            tbStockDetail.cnnQuantity         = cnnQuantity;
            tbStockDetail.cnvcMainComUnitCode = cnvcMainComUnitCode;
            tbStockDetail.cnnMainQuantity     = cnnMainQuantity;
            tbStockDetail.cnnPrice            = cnnPrice;
            tbStockDetail.cnnAmount           = cnnAmount;
            tbStockDetail.cndOperDate         = tbStockMain.cndCreateDate;
            tbStockDetail.cnvcOper            = tbStockMain.cnvcCreaterId;
            tbStockDetail.cnvcOperName        = tbStockMain.cnvcCreaterName;

            amscm.tbStockDetail.Add(tbStockDetail);
            amscm.SaveChanges();

            AMSCM.Models.tbStockDetailLog tbStockDetailLog = new AMSCM.Models.tbStockDetailLog();
            ServiceHelper.SetEntity <AMSCM.Models.tbStockDetail, AMSCM.Models.tbStockDetailLog>(tbStockDetail, tbStockDetailLog);
            amscm.tbStockDetailLog.Add(tbStockDetailLog);
        }
        private string cancelBalance(HttpContext context)
        {
            try
            {
                //using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(0, 20, 0)))
                //{
                using (AMSCM.Models.AMSCM amscm = new AMSCM.Models.AMSCM())
                {
                    int icnnYear  = Convert.ToInt32(context.Request.Form["cnnYear"]);
                    int icnnMonth = Convert.ToInt32(context.Request.Form["cnnMonth"]);
                    int icount    = (from d in amscm.tbMonthlyBalance
                                     where d.cnnYear >= icnnYear && d.cnnMonth > icnnMonth && d.cnbIsBalance
                                     select d).Count();
                    if (icount > 0)
                    {
                        return(ServiceHelper.JsonSerializer <JEasyUIResult>(new JEasyUIResult(false, "请先取消以前月份月结!")));
                    }
                    AMSCM.Models.tbMonthlyBalance tbMonthlyBalance = amscm.tbMonthlyBalance.Find(icnnYear, 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;

                    //amscm.SaveChanges();
                    AMSCM.Models.tbMonthlyBalanceLog tbMonthlyBalanceLog = new AMSCM.Models.tbMonthlyBalanceLog();
                    ServiceHelper.SetEntity <AMSCM.Models.tbMonthlyBalance, AMSCM.Models.tbMonthlyBalanceLog>(tbMonthlyBalance, tbMonthlyBalanceLog);
                    amscm.tbMonthlyBalanceLog.Add(tbMonthlyBalanceLog);

                    int         icnnCheckStatus = (int)StockStatus.Check;
                    List <long> lcnnDetailId    = (from main in amscm.tbStockMain
                                                   join d in amscm.tbStockDetail 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 == null?0:detail.cnnDetailId).Distinct().ToList();
                    foreach (long cnnDetailId in lcnnDetailId)
                    {
                        if (cnnDetailId == 0)
                        {
                            continue;
                        }
                        AMSCM.Models.tbStockDetail tbStockDetail = amscm.tbStockDetail.Find(cnnDetailId);
                        tbStockDetail.cnnPrice  = 0;
                        tbStockDetail.cnnAmount = 0;

                        AMSCM.Models.tbStockDetailLog tbStockDetailLog = new AMSCM.Models.tbStockDetailLog();
                        ServiceHelper.SetEntity <AMSCM.Models.tbStockDetail, AMSCM.Models.tbStockDetailLog>(tbStockDetail, tbStockDetailLog);
                        amscm.tbStockDetailLog.Add(tbStockDetailLog);
                        //amscm.SaveChanges();
                    }
                    int inextcnnYear  = icnnYear;
                    int inextcnnMonth = icnnMonth + 1;
                    if (icnnMonth == 12)
                    {
                        inextcnnYear += 1;
                        inextcnnMonth = 1;
                    }
                    var q = (from main in amscm.tbStockMain
                             join d in amscm.tbStockDetail 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)
                        {
                            AMSCM.Models.tbStockMain tbStockMain = amscm.tbStockMain.Find(cnnMainId);
                            tbStockMain.cnnStatus = (int)StockStatus.Delete;
                            amscm.tbStockMain.Remove(tbStockMain);

                            AMSCM.Models.tbStockMainLog tbStockMainLog = new AMSCM.Models.tbStockMainLog();
                            ServiceHelper.SetEntity <AMSCM.Models.tbStockMain, AMSCM.Models.tbStockMainLog>(tbStockMain, tbStockMainLog);
                            amscm.tbStockMainLog.Add(tbStockMainLog);
                            //amscm.SaveChanges();
                        }
                    }
                    if (q2.Count > 0)
                    {
                        foreach (long cnnDetailId in q2)
                        {
                            AMSCM.Models.tbStockDetail tbStockDetail = amscm.tbStockDetail.Find(cnnDetailId);
                            amscm.tbStockDetail.Remove(tbStockDetail);

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