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 void Compute(DXInfo.Models.tbMonthlyBalance tbMonthlyBalance, string strOperId, string strOperName) { int icnnCheckStatus = (int)StockStatus.Check; List <tbStockMainAndDetail> ltbStockMainAndDetail = (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 orderby main.cnnMainId select new tbStockMainAndDetail { cnnMainId = main.cnnMainId, cnvcWhCode = main.cnvcWhCode, cnvcDeptId = main.cnvcDeptId, cnnOperType = main.cnnOperType, cnnYear = main.cnnYear, cnnMonth = main.cnnMonth, 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 }).ToList(); var ltbInventory = (from d in Uow.tbInventory.GetAll() join d1 in Uow.tbComputationUnit.GetAll() on d.cnvcSTComUnitCode equals d1.cnvcComunitCode into dd1 from dd1s in dd1.DefaultIfEmpty() join d2 in Uow.tbProductClass.GetAll() on d.cnvcInvCCode equals d2.cnvcProductClassCode into dd2 from dd2s in dd2.DefaultIfEmpty() join d3 in Uow.tbComputationUnit.GetAll() 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(); //利用期初、采购的数据计算成本 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) }).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.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 <DXInfo.Models.tbBillOfMaterials> lbom = ServiceHelper.getBOM(Uow); List <tbStockMainAndDetail> lm = new List <tbStockMainAndDetail>(); foreach (string strInvCode in lInv) { if (!string.IsNullOrEmpty(strInvCode)) { //获取所有子件 List <DXInfo.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.cnnAmount / md.cnnQuantity; } }); //3、更新成本 ltbStockMainAndDetail.ForEach(delegate(tbStockMainAndDetail md) { if (md.cnnOperType != 0 && md.cnnOperType != 1 && md.cnnDetailId.HasValue) { DXInfo.Models.tbStockDetail tbStockDetail = Uow.tbStockDetail.GetById(g => g.cnnDetailId == md.cnnDetailId); tbStockDetail.cnnPrice = md.cnnPrice.Value; tbStockDetail.cnnAmount = md.cnnAmount.Value; } }); //计算下月的期初库存 var g3 = (from d in ltbStockMainAndDetail join d1 in ltbInventory on d.cnvcInvCode equals d1.cnvcInvCode into dd1 from dd1s in dd1.DefaultIfEmpty() group d by new { d.cnvcInvCode, d.cnvcWhCode, d.cnvcDeptId, d.cnvcMainComUnitCode, cnvcSTComUnitCode = dd1s == null ? "" : dd1s.cnvcSTComUnitCode, cniChangRate = dd1s == null?0:dd1s.cniChangRate } into g select new { cnvcWhCode = g.Key.cnvcWhCode, cnvcDeptId = g.Key.cnvcDeptId, cnvcInvCode = g.Key.cnvcInvCode, cnvcMainComUnitCode = g.Key.cnvcMainComUnitCode, cnvcComUnitCode = g.Key.cnvcSTComUnitCode, cniChangRate = g.Key.cniChangRate, cnnAmount = g.Sum(s => s.cnnAmount), cnnMainQuantity = g.Sum(s => s.cnnMainQuantity), cnnQuantity = g.Key.cniChangRate == 0?0:g.Sum(s => s.cnnMainQuantity) / g.Key.cniChangRate, cnnPrice = g.Sum(s => s.cnnMainQuantity) == 0?0:(g.Sum(s => s.cnnAmount) / g.Sum(s => s.cnnMainQuantity)) * g.Key.cniChangRate }).ToList(); var g4 = (from d in g3 select new { d.cnvcWhCode, d.cnvcDeptId }).Distinct(); int icnnYear = tbMonthlyBalance.cnnYear; int icnnMonth = tbMonthlyBalance.cnnMonth; if (icnnMonth == 12) { icnnYear += 1; icnnMonth = 1; } else { icnnMonth += 1; } DateTime cndBusinessDate = new DateTime(icnnYear, icnnMonth, 1); foreach (var g5 in g4) { var g6 = (from d in g3 where d.cnvcDeptId == g5.cnvcDeptId && d.cnvcWhCode == g5.cnvcWhCode && d.cnvcInvCode != null select d).ToList(); if (g6.Count > 0) { DXInfo.Models.tbStockMain tbStockMain = AddStockMain(g5.cnvcWhCode, g5.cnvcDeptId, strOperId, strOperName, 0, cndBusinessDate); foreach (var g7 in g6) { if (g7.cnnQuantity < 0 && g7.cnnAmount < 0 && g7.cnnMainQuantity < 0) { DXInfo.Models.tbStockMain tbStockMain2 = AddStockMain(g5.cnvcWhCode, g5.cnvcDeptId, strOperId, strOperName, 4, cndBusinessDate); ServiceHelper.AddStockDetal(Uow, tbStockMain2, g7.cnvcInvCode, g7.cnvcComUnitCode, -g7.cnnQuantity.Value, g7.cnvcMainComUnitCode, -g7.cnnMainQuantity.Value, g7.cnnPrice.Value, -g7.cnnAmount.Value); ServiceHelper.AddStockDetal(Uow, tbStockMain, g7.cnvcInvCode, g7.cnvcComUnitCode, 0, g7.cnvcMainComUnitCode, 0, g7.cnnPrice.Value, 0); } else { ServiceHelper.AddStockDetal(Uow, tbStockMain, g7.cnvcInvCode, g7.cnvcComUnitCode, g7.cnnQuantity.Value, g7.cnvcMainComUnitCode, g7.cnnMainQuantity.Value, g7.cnnPrice.Value, g7.cnnAmount.Value); } } } } }