Exemplo n.º 1
0
 private string UpdateCost(HttpContext context)
 {
     try
     {
         string strOperId           = context.User.Identity.Name;
         CMSMStruct.LoginStruct ls1 = (CMSMStruct.LoginStruct)context.Session["Login"];
         string strOperName         = ls1.strOperName;
         int    icnnYear            = Convert.ToInt32(context.Request.Form["cnnYear"]);
         int    icnnMonth           = Convert.ToInt32(context.Request.Form["cnnMonth"]);
         AMSCM.Models.tbMonthlyBalance tbMonthlyBalance = new AMSCM.Models.tbMonthlyBalance();
         tbMonthlyBalance.cnnYear  = icnnYear;
         tbMonthlyBalance.cnnMonth = icnnMonth;
         using (AMSCM.Models.AMSCM amscm = new AMSCM.Models.AMSCM())
         {
             Compute(amscm, tbMonthlyBalance, strOperId, strOperName);
         }
     }
     catch (DbUpdateException dex)
     {
         ExceptionPolicy.HandleException(dex, ServiceHelper.ExceptionPolicy);
         return(ServiceHelper.JsonSerializer <JEasyUIResult>(new JEasyUIResult(false, dex.Message)));
     }
     catch (Exception ex)
     {
         ExceptionPolicy.HandleException(ex, ServiceHelper.ExceptionPolicy);
         return(ServiceHelper.JsonSerializer <JEasyUIResult>(new JEasyUIResult(false, ex.Message)));
     }
     return(ServiceHelper.JsonSerializer <JEasyUIResult>(new JEasyUIResult(true, "")));
 }
        private string newtbMonthlyBalance(HttpContext context)
        {
            try
            {
                using (AMSCM.Models.AMSCM amscm = new AMSCM.Models.AMSCM())
                {
                    AMSCM.Models.tbMonthlyBalance tbMonthlyBalance = new AMSCM.Models.tbMonthlyBalance();
                    tbMonthlyBalance.cnnYear      = Convert.ToInt32(context.Request.Form["cnnYear"]);
                    tbMonthlyBalance.cnnMonth     = Convert.ToInt32(context.Request.Form["cnnMonth"]);
                    tbMonthlyBalance.cnbIsBalance = false;

                    tbMonthlyBalance.cndCreateDate = DateTime.Now;
                    tbMonthlyBalance.cnvcCreater   = context.User.Identity.Name;
                    CMSMStruct.LoginStruct ls1 = (CMSMStruct.LoginStruct)context.Session["Login"];
                    tbMonthlyBalance.cnvcCreaterName = ls1.strOperName;

                    //tbMonthlyBalance.cndModifyDate = tbMonthlyBalance.cndCreateDate;
                    //tbMonthlyBalance.cnvcModifier = tbMonthlyBalance.cnvcCreater;
                    //tbMonthlyBalance.cnvcModifierName = tbMonthlyBalance.cnvcCreaterName;
                    amscm.tbMonthlyBalance.Add(tbMonthlyBalance);

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

                    amscm.SaveChanges();
                }
            }
            catch (DbUpdateException dex)
            {
                ExceptionPolicy.HandleException(dex, ServiceHelper.ExceptionPolicy);
                return(ServiceHelper.JsonSerializer <JEasyUIResult>(new JEasyUIResult(false, dex.Message)));
            }
            return(ServiceHelper.JsonSerializer <JEasyUIResult>(new JEasyUIResult(true, "")));
        }
        private string balance(HttpContext context)
        {
            try
            {
                using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(0, 10, 0)))
                {
                    using (AMSCM.Models.AMSCM amscm = new AMSCM.Models.AMSCM())
                    {
                        //amscm.Database.Connection.ConnectionTimeout = 600;

                        //更新月结表
                        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 = true;

                        string strOperId           = context.User.Identity.Name;
                        CMSMStruct.LoginStruct ls1 = (CMSMStruct.LoginStruct)context.Session["Login"];
                        string strOperName         = ls1.strOperName;
                        tbMonthlyBalance.cndBalanceDate = DateTime.Now;
                        tbMonthlyBalance.cnvcBalancer   = strOperId;

                        tbMonthlyBalance.cnvcBalancerName = strOperName;


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

                        //更新盘点和领料单的成本
                        Compute(amscm, tbMonthlyBalance, strOperId, strOperName);
                        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, "")));
        }
Exemplo n.º 4
0
        private void Compute(AMSCM.Models.AMSCM amscm, AMSCM.Models.tbMonthlyBalance tbMonthlyBalance, string strOperId, string strOperName)
        {
            int icnnCheckStatus = (int)StockStatus.Check;
            List <tbStockMainAndDetail> ltbStockMainAndDetail = (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
                                                                 orderby main.cnnMainId
                                                                 select new tbStockMainAndDetail
            {
                cnnMainId = main.cnnMainId,
                //cnvcSupplierCode = main.cnvcSupplierCode,
                cnvcWhCode = main.cnvcWhCode,
                cnvcDeptId = main.cnvcDeptId,
                cnnOperType = main.cnnOperType,
                //cndCreateDate = main.cndCreateDate,
                //cnvcCreaterId = main.cnvcCreaterId,
                //cnvcCreaterName = main.cnvcCreaterName,
                //cndCheckDate = main.cndCheckDate,
                //cnvcCheckerId = main.cnvcCheckerId,
                //cnvcCheckerName = main.cnvcCheckerName,
                //cndBusinessDate = main.cndBusinessDate,
                cnnYear = main.cnnYear,
                cnnMonth = main.cnnMonth,
                //cnnStatus = main.cnnStatus,
                //cnvcComments = main.cnvcComments,
                cnnDetailId = detail.cnnDetailId,
                cnvcInvCode = detail.cnvcInvCode,
                cnvcComUnitCode = detail.cnvcComUnitCode,
                cnnQuantity = detail.cnnQuantity,
                cnvcMainComUnitCode = detail.cnvcMainComUnitCode,
                cnnMainQuantity = detail.cnnMainQuantity,
                cnnPrice = detail.cnnPrice,
                cnnAmount = detail.cnnAmount,
                isHave = false
                         //cnnMainPrice = detail.cnnPrice==0?0:detail.cnnAmount / detail.cnnPrice
            }).ToList();
            var ltbInventory = (from d in amscm.tbInventory
                                join d1 in amscm.tbComputationUnit on d.cnvcSTComUnitCode equals d1.cnvcComunitCode into dd1
                                from dd1s in dd1.DefaultIfEmpty()

                                join d2 in amscm.tbProductClass on d.cnvcInvCCode equals d2.cnvcProductClassCode into dd2
                                from dd2s in dd2.DefaultIfEmpty()

                                join d3 in amscm.tbComputationUnit on d.cnvcProduceUnitCode equals d3.cnvcComunitCode into dd3
                                from dd3s in dd3.DefaultIfEmpty()

                                select new
            {
                d.cnvcInvCode,
                dd2s.cnvcProductType,
                d.cnvcProduceUnitCode,
                d.cnvcSTComUnitCode,
                cniChangRate = dd1s == null ? 0 : dd1s.cniChangRate,
                cniChangeRate2 = dd3s == null ? 0 : dd3s.cniChangRate
            }).ToList();
            //foreach (tbStockMainAndDetail tbStockMainAndDetail in ltbStockMainAndDetail)
            //{
            //    tbStockMainAndDetail.cnnMainPrice = tbStockMainAndDetail.cnnAmount / tbStockMainAndDetail.cnnMainQuantity;
            //}
            //利用期初、采购的数据计算成本
            var g1 = (from d in ltbStockMainAndDetail
                      where d.cnnOperType == 0 || d.cnnOperType == 1
                      group d by new { d.cnvcInvCode, d.cnvcWhCode, d.cnvcDeptId } into g
                      select new
            {
                cnvcInvCode = g.Key.cnvcInvCode,
                cnvcWhCode = g.Key.cnvcWhCode,
                cnvcDeptId = g.Key.cnvcDeptId,
                //cnnAvgPrice = g.Sum(s => s.cnnAmount) / g.Sum(s => s.cnnMainQuantity)
                cnnAvgPrice = g.Sum(s => s.cnnMainQuantity) == 0?g.Sum(s => s.cnnAmount):
                              g.Sum(s => s.cnnAmount) / g.Sum(s => s.cnnMainQuantity)
            }).ToList();

            //var q1 = (from d in ltbStockMainAndDetail where d.cnnOperType==4 || d.cnnOperType==6 select d).ToList();
            //1、已有的取出成本
            foreach (var g2 in g1)
            {
                ltbStockMainAndDetail.ForEach(delegate(tbStockMainAndDetail md)
                {
                    if (md.cnvcInvCode == g2.cnvcInvCode && md.cnvcWhCode == g2.cnvcWhCode &&
                        md.cnvcDeptId == g2.cnvcDeptId &&
                        md.cnnOperType != 0 && md.cnnOperType != 1
                        )
                    {
                        md.cnnMainPrice = g2.cnnAvgPrice;
                        md.cnnPrice     = md.cnnMainPrice * (md.cnnQuantity == 0?1:md.cnnMainQuantity / md.cnnQuantity);
                        md.cnnAmount    = md.cnnMainPrice * md.cnnMainQuantity;
                        md.isHave       = true;
                    }
                });
            }
            //2、根据配方算出用量
            var lInv = (from d in ltbStockMainAndDetail where d.cnnOperType != 0 && d.cnnOperType != 1 && !d.isHave select d.cnvcInvCode).Distinct().ToList();
            List <AMSCM.Models.tbBillOfMaterials> lbom = ServiceHelper.getBOM(amscm);

            List <tbStockMainAndDetail> lm = new List <tbStockMainAndDetail>();

            foreach (string strInvCode in lInv)
            {
                if (!string.IsNullOrEmpty(strInvCode))
                {
                    //获取所有子件
                    List <AMSCM.Models.tbBillOfMaterials> lComponentInv = ServiceHelper.ProcBOM(lbom, strInvCode);

                    //获取子件生产单位数量
                    var lcis = from d in lComponentInv
                               group d by d.cnvcComponentInvCode into g
                               select new { cnvcInvCode = g.Key, cnnQuantity = g.Sum(s => s.cnnBaseQtyN) };
                    //获取主计量单位成本
                    decimal dPrice = (from d in lcis
                                      join d1 in ltbInventory on d.cnvcInvCode equals d1.cnvcInvCode into dd1
                                      from dd1s in dd1.DefaultIfEmpty()

                                      join d2 in g1 on d.cnvcInvCode equals d2.cnvcInvCode into dd2
                                      from dd2s in dd2.DefaultIfEmpty()
                                      select d.cnnQuantity *(dd1s == null ? 0 : dd1s.cniChangeRate2) * (dd2s == null || !dd2s.cnnAvgPrice.HasValue ? 0 : dd2s.cnnAvgPrice.Value)).Sum();
                    lm.Add(new tbStockMainAndDetail()
                    {
                        cnvcInvCode = strInvCode, cnnMainPrice = dPrice
                    });
                }
            }
            ;
            ltbStockMainAndDetail.ForEach(delegate(tbStockMainAndDetail md)
            {
                if (md.cnnOperType != 0 && md.cnnOperType != 1 && !md.isHave)
                {
                    decimal?dcnnMainPrice = (from d in lm where d.cnvcInvCode == md.cnvcInvCode select d.cnnMainPrice).FirstOrDefault();
                    md.cnnMainPrice       = dcnnMainPrice;
                    md.cnnAmount          = dcnnMainPrice * md.cnnMainQuantity;
                    md.cnnPrice           = md.cnnQuantity == 0?md.cnnAmount:md.cnnAmount / md.cnnQuantity;
                }
            });
            //3、更新成本
            ltbStockMainAndDetail.ForEach(delegate(tbStockMainAndDetail md)
            {
                if (md.cnnOperType != 0 && md.cnnOperType != 1 && md.cnnDetailId.HasValue)
                {
                    AMSCM.Models.tbStockDetail tbStockDetail = amscm.tbStockDetail.Find(md.cnnDetailId);
                    tbStockDetail.cnnPrice  = md.cnnPrice.Value;
                    tbStockDetail.cnnAmount = md.cnnAmount.Value;
                }
            });
            amscm.SaveChanges();
        }
        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, "")));
        }