Example #1
0
        public void TryCalculate(string multiregion, DateTime startDate, DateTime endDate, int kbCalc, User calcUser)
        {
            #region 删除同批号的,按照区域,开始日期,结束日期
            string batchno = KanbanUtility.GetBatchNo(multiregion, null, null, startDate, endDate, kbCalc, calcUser);
            this.genericMgr.Delete("from KanbanCalc where BatchNo = ?", batchno, NHibernate.NHibernateUtil.String);
            genericMgr.FlushSession();
            genericMgr.CleanSession();
            #endregion

            List<KanbanCalc> kanbanCalcList = new List<KanbanCalc>();
            IList<KanbanCalc> lerror = new List<KanbanCalc>();

            IList<Region> calcRegions = new List<Region>();
            DateTime dateTimeNow = DateTime.Now;

            #region 检查区域的标准和特殊代码有没有设置
            string[] regions = multiregion.Split(',');
            for (int ir = 0; ir < regions.Length; ir++)
            {
                Region r = this.genericMgr.FindAll<Region>("from Region where Code = ? ", regions[ir]).FirstOrDefault();

                //if (String.IsNullOrEmpty(r.KbStandardCode) || String.IsNullOrEmpty(r.KbAlternativeCode))
                //{
                //    throw new BusinessException("没找到区域" + r.Code + "的看板代码和异常代码!");
                //}
                calcRegions.Add(r);
            }
            #endregion

            if (calcRegions.Count() > 0)
            {
                IList<ShiftMaster> shiftMasterList = genericMgr.FindAll<ShiftMaster>(); //先这样吧,全部的班次,反正也不多
                foreach (Region calcRegion in calcRegions)
                {
                    #region 把生效时间未到和待冻结的都删掉

                    genericMgr.FindAllWithNativeSql<KanbanCard>("Delete from KB_KanbanCard  where Region = ? and KBCalc = ? and (EffDate > ? or FreezeDate < ?) ", new object[] { calcRegion.Code, kbCalc, dateTimeNow, dateTimeNow });
                    genericMgr.FindAllWithNativeSql<KanbanCard>("Update KB_KanbanCard set TotalCount = k.TotalCount from (select FlowDetId, max(Number) as TotalCount from KB_KanbanCard where Region = ? and KBCalc = ? group by FlowDetId ) k where k.FlowDetId = KB_KanbanCard.FlowDetId and KB_KanbanCard.Region = ? and KB_KanbanCard.KBCalc = ?", new object[] { calcRegion.Code, kbCalc, calcRegion.Code, kbCalc });
                    genericMgr.FindAllWithNativeSql<FlowDetail>("Update SCM_FlowDet set CycloidAmount =  c.TotalCount from KB_KanbanCard c where c.FlowDetId = SCM_FlowDet.Id");
                    genericMgr.FindAllWithNativeSql<FlowDetail>("Update SCM_FlowDet set CycloidAmount = 0 where not exists (select 1 from KB_KanbanCard c where c.FlowDetId = SCM_FlowDet.Id) and CycloidAmount>0");
                    genericMgr.FlushSession();
                    genericMgr.CleanSession();

                    #endregion

                    #region 取此区域下所有的路线
                    //WorkingCalendar nSub1 = workingCalendarMgr.GetLastWorkingCalendar(startDate, region, 7);
                    IList<FlowMaster> flowList = genericMgr.FindEntityWithNativeSql<FlowMaster>(@"select c.* from scm_flowmstr c inner join scm_flowstrategy as g on c.Code = g.Flow where c.IsActive = ? and  c.PartyTo = ? and g.Strategy = ? and g.KBCalc = ?", new object[] { true, calcRegion.Code, (int)com.Sconit.CodeMaster.FlowStrategy.KB, kbCalc });
                    List<ItemDailyConsume> itemDailyConsumeList = new List<ItemDailyConsume>();
                    IList<WorkingCalendar> workingCalendatList = genericMgr.FindAll<WorkingCalendar>(" from WorkingCalendar w where w.FlowStrategy = ? and w.Region = ? and w.WorkingDate >= ? and w.WorkingDate <= ?", new object[] { (int)com.Sconit.CodeMaster.FlowStrategy.KB, calcRegion.Code, startDate, endDate });
                    IList<Party> partyList = genericMgr.FindAll<Party>();
                    IList<LocationBinItem> locationBinItemList = genericMgr.FindEntityWithNativeSql<LocationBinItem>(@"select l.* from md_locationbinitem l inner join md_location d on l.Location = d.Code where d.Region = ?", calcRegion.Code);
                    //IList<Item> kitItemList = genericMgr.FindAll<Item>(" from Item i where i.IsKit = ?",true);
                    IList<ItemTrace> itemTraceList = genericMgr.FindAll<ItemTrace>();
                    IList<Item> itemList = genericMgr.FindEntityWithNativeSql<Item>(@"select i.* from md_item i inner join scm_flowdet d on i.code = d.item inner join scm_flowmstr c on d.flow = c.code inner join scm_flowstrategy as g on c.Code = g.Flow where c.IsActive = ? and  c.PartyTo = ? and g.Strategy = ? and g.KBCalc = ?", new object[] { true, calcRegion.Code, (int)com.Sconit.CodeMaster.FlowStrategy.KB, kbCalc });
                    IList<Supplier> supplierList = genericMgr.FindAll<Supplier>();
                    IList<ItemKit> itemKitList = genericMgr.FindAll<ItemKit>();
                    #endregion

                    if (flowList != null && flowList.Count() > 0)
                    {
                        #region 获取站别日用量
                        if (kbCalc == (int)com.Sconit.CodeMaster.KBCalculation.Normal)
                        {
                            //采购的
                            IList<ItemDailyConsume> procurementItemDailyConsumeList = this.genericMgr.FindEntityWithNativeSql<ItemDailyConsume>(@"select max(ic.id) as id,ic.item,max(ic.itemdesc) as itemdesc, ic.location,max(qty) as qty,max(ic.consumedate) as consumedate,max(ic.substitutegroup) as substitutegroup,max(ic.multisupplygroup) as multisupplygroup,max(ic.createdate) as createdate,max(ic.originalqty) as originalqty
                                                                                from cust_itemdailyconsume ic 
                                                                                inner join scm_flowdet fd on ic.item = fd.item 
                                                                                inner join scm_flowmstr fm on fd.flow = fm.code 
                                                                                inner join scm_flowstrategy fg on fm.code = fg.flow
                                                                                inner join prd_workingcalendar pw on ic.ConsumeDate = pw.workingdate 
                                                                                where fm.isactive = ? and ic.Location = fm.LocTo and fm.Type = ?
                                                                                and fm.FlowStrategy = ? and fg.kbcalc = ? 
                                                                                and ic.ConsumeDate >= ? and ic.ConsumeDate <= ?
                                                                                and fm.PartyTo = ? and pw.type = ? and pw.flowstrategy = ? and pw.region = fm.PartyTo
                                                                                and ic.qty > 0
                                                                                group by ic.location,ic.item,pw.shift",
                                                                                new object[] { true, (int)com.Sconit.CodeMaster.OrderType.Procurement, (int)com.Sconit.CodeMaster.FlowStrategy.KB, kbCalc, startDate, endDate, calcRegion.Code, (int)com.Sconit.CodeMaster.WorkingCalendarType.Work, (int)com.Sconit.CodeMaster.FlowStrategy.KB });
                            //移库的,有些是用来源库位来算的,如KD件,有些使用目的库位来算的,如闭口订单,真变态
                            IList<ItemDailyConsume> transferItemDailyConsumeList = this.genericMgr.FindEntityWithNativeSql<ItemDailyConsume>(@"select max(ic.id) as id,ic.item,max(ic.itemdesc) as itemdesc, ic.location,max(qty) as qty,max(ic.consumedate) as consumedate,max(ic.substitutegroup) as substitutegroup,max(ic.multisupplygroup) as multisupplygroup,max(ic.createdate) as createdate,max(ic.originalqty) as originalqty
                                                                                from cust_itemdailyconsume ic 
                                                                                inner join scm_flowdet fd on ic.item = fd.item 
                                                                                inner join scm_flowmstr fm on fd.flow = fm.code 
                                                                                inner join scm_flowstrategy fg on fm.code = fg.flow
                                                                                inner join prd_workingcalendar pw on ic.ConsumeDate = pw.workingdate 
                                                                                where fm.isactive = ? and (ic.Location = fm.LocTo or ic.Location = fm.LocFrom) and fm.Type = ?
                                                                                and fm.FlowStrategy = ? and fg.kbcalc = ? 
                                                                                and ic.ConsumeDate >= ? and ic.ConsumeDate <= ?
                                                                                and fm.PartyTo = ? and pw.type = ? and pw.flowstrategy = ? and pw.region = fm.PartyTo
                                                                                and ic.qty > 0
                                                                                group by ic.location,ic.item,pw.shift",
                                                                                new object[] { true, (int)com.Sconit.CodeMaster.OrderType.Transfer, (int)com.Sconit.CodeMaster.FlowStrategy.KB, kbCalc, startDate, endDate, calcRegion.Code, (int)com.Sconit.CodeMaster.WorkingCalendarType.Work, (int)com.Sconit.CodeMaster.FlowStrategy.KB });

                            if (procurementItemDailyConsumeList != null && procurementItemDailyConsumeList.Count > 0)
                            {
                                itemDailyConsumeList.AddRange(procurementItemDailyConsumeList);
                            }

                            if (transferItemDailyConsumeList != null && transferItemDailyConsumeList.Count > 0)
                            {
                                itemDailyConsumeList.AddRange(transferItemDailyConsumeList);
                            }
                        }
                        else if (kbCalc == (int)com.Sconit.CodeMaster.KBCalculation.CatItem)
                        {
                            //生产,因为冲压送焊装的部分站别日用量在焊装,不是冲压,不考虑库位,按照生产线明细匹配
                            IList<ItemDailyConsume> productionItemDailyConsumeList = this.genericMgr.FindEntityWithNativeSql<ItemDailyConsume>(@"select ic.* from cust_itemdailyconsume ic 
                                                                                inner join scm_flowdet fd on ic.item = fd.item inner join scm_flowmstr fm on fd.flow = fm.code inner join scm_flowstrategy fg on fm.code = fg.flow
                                                                                where fm.isactive = ? and fm.FlowStrategy = ? and fg.kbcalc = ? and ic.ConsumeDate >= ? and ic.ConsumeDate <= ? and fm.PartyTo = ? and ic.Qty > 0",
                                                                                  new object[] { true, (int)com.Sconit.CodeMaster.FlowStrategy.KB, kbCalc, startDate, endDate, calcRegion.Code });
                            if (productionItemDailyConsumeList != null && productionItemDailyConsumeList.Count > 0)
                            {
                                itemDailyConsumeList.AddRange(productionItemDailyConsumeList);
                            }
                        }
                        #endregion

                    }

                    foreach (FlowMaster flow in flowList)
                    {
                        #region 物流
                        if (kbCalc == (int)com.Sconit.CodeMaster.KBCalculation.Normal)
                        {
                            IList<FlowShiftDetail> flowShiftDetailList = genericMgr.FindAll<FlowShiftDetail>(" from FlowShiftDetail f where f.Flow = ?", flow.Code);
                            IList<FlowDetail> flowDetailList = genericMgr.FindAll<FlowDetail>(" from FlowDetail d where d.Flow = ? and d.IsRejectInspect = ?", new object[] { flow.Code, false });
                            com.Sconit.Entity.SCM.FlowStrategy flowStrategy = genericMgr.FindById<com.Sconit.Entity.SCM.FlowStrategy>(flow.Code);

                            #region 冻结日期和投出日期时间,冻结取计算时段第一个时段不能交进来,投出取计算前一个工作日的倒数4个时段作为交货时段,先做倒数第4个时段好了,平均分配太麻烦
                            DateTime freezeDate = DateTime.Parse("2999-1-1 00:00:00");
                            DateTime effDate = DateTime.Parse("1900-1-1 00:00:00");
                            #endregion

                            #region 按路线明细计算看板
                            //   string matchLocation = flow.Type == com.Sconit.CodeMaster.OrderType.Procurement ? flow.LocationTo : flow.LocationFrom;
                            foreach (FlowDetail flowDetail in flowDetailList)
                            {
                                IList<ItemDailyConsume> flowDetailItemDailyConsumeList = itemDailyConsumeList.Where(p => p.Item == flowDetail.Item && (p.Location == flow.LocationFrom || p.Location == flow.LocationTo)).ToList();
                                //IList<KanbanCard> existKanbanCardList = genericMgr.FindAll<KanbanCard>("from KanbanCard c where c.FlowDetailId = ?", flowDetail.Id);
                                int existKanbanNum = flowDetail.CycloidAmount;

                                decimal calculatedKanbanNum = 0;
                                DateTime? calcDate = null;
                                Decimal calcQty = 0;
                                string groupNo = string.Empty;
                                int tiao = 0;

                                IList<string> shiftList = workingCalendatList.Select(i => i.Shift).Distinct().ToList();

                                #region 计算
                                if (flowDetailItemDailyConsumeList != null && flowDetailItemDailyConsumeList.Count > 0)
                                {
                                    foreach (ItemDailyConsume idc in flowDetailItemDailyConsumeList)
                                    {
                                        WorkingCalendar wc = workingCalendatList.Where(w => w.Region == flow.PartyTo && w.WorkingDate == idc.ConsumeDate).ToList().FirstOrDefault();

                                        if (wc != null && wc.Type == (int)com.Sconit.CodeMaster.WorkingCalendarType.Work)
                                        {
                                            int qtbM = flowShiftDetailList.Where(f => f.Shift == wc.Shift).Count();
                                            int workTimeNum = shiftMasterList.Where(s => s.Code == wc.Shift).FirstOrDefault().ShiftCount;
                                            if (qtbM > 0 && workTimeNum > 0 && flowDetail.UnitCount > 0)
                                            {
                                                decimal dailyKanbanNum = Convert.ToDecimal((idc.Qty * ((1 + flowStrategy.LeadTime) * workTimeNum + (flowDetail.SafeStock == null ? 0 : flowDetail.SafeStock.Value) * qtbM)) / (qtbM * workTimeNum * flowDetail.UnitCount));

                                                if (dailyKanbanNum > calculatedKanbanNum)
                                                {
                                                    calculatedKanbanNum = dailyKanbanNum;
                                                    calcQty = idc.Qty;
                                                    calcDate = idc.ConsumeDate;
                                                    groupNo = idc.MultiSupplyGroup;
                                                    tiao = qtbM;
                                                }
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    #region 本次无日用量的,应该所有的都冻结
                                    #endregion
                                }
                                #endregion

                                #region 记录计算结果
                                KanbanCalc kanbanCalc = new KanbanCalc();
                                kanbanCalc.Flow = flow.Code;
                                kanbanCalc.KBCalc = (com.Sconit.CodeMaster.KBCalculation)kbCalc;
                                kanbanCalc.CalcConsumeDate = calcDate;
                                kanbanCalc.CalcConsumeQty = calcQty;
                                kanbanCalc.CalcKanbanNum = calculatedKanbanNum;
                                kanbanCalc.Flow = flow.Code;
                                kanbanCalc.FlowDetailId = flowDetail.Id;
                                kanbanCalc.Item = flowDetail.Item;

                                Item item = itemList.Where(k => k.Code == kanbanCalc.Item).FirstOrDefault();
                                kanbanCalc.ItemDescription = item.Description;
                                //  kanbanCalc.Container = item.UnitCountDescription;
                                if (item.IsKit)
                                {
                                    kanbanCalc.IsKit = item.IsKit;
                                    kanbanCalc.KitCount = itemKitList.Where(p => p.KitItem == item.Code).Count();
                                }
                                ItemTrace itemTrace = itemTraceList.Where(t => t.Item == flowDetail.Item).FirstOrDefault();
                                if (itemTrace != null)
                                {
                                    kanbanCalc.IsTrace = true;
                                }
                                Supplier supplier = supplierList.Where(s => s.Code == flow.PartyFrom).FirstOrDefault();
                                //if (supplier != null)
                                //{
                                //    kanbanCalc.LogisticCenterCode = supplier.LogisticsCentre;
                                //    kanbanCalc.LogisticCenterName = supplier.LogisticsCentreName;
                                //}

                                //kanbanCalc.GroupNo = flowDetail.GroupNo;
                                //kanbanCalc.GroupDesc = flowDetail.GroupDesc;
                                kanbanCalc.KanbanNum = existKanbanNum;
                                kanbanCalc.Qty = flowDetail.UnitCount;
                                kanbanCalc.KanbanDeltaNum = (int)Math.Ceiling(kanbanCalc.CalcKanbanNum < 1 ? 1 : kanbanCalc.CalcKanbanNum) - existKanbanNum;
                                kanbanCalc.MultiSupplyGroup = groupNo;
                                kanbanCalc.Region = calcRegion.Code;
                                kanbanCalc.RegionName = calcRegion.Name;
                                kanbanCalc.Supplier = flow.PartyFrom;
                                var sup = partyList.Where(p => p.Code == flow.PartyFrom).FirstOrDefault();
                                kanbanCalc.SupplierName = sup.Name;
                                if (sup is Supplier)
                                {
                                    Supplier su = sup as Supplier;
                                    kanbanCalc.SupplierName = string.IsNullOrEmpty(su.ShortCode) ? su.Name.Substring(0, 4) : su.ShortCode;
                                }

                                // kanbanCalc.QiTiaoBian = flowStrategy.QiTiaoBian;
                                kanbanCalc.OpRef = flowDetail.BinTo;
                                kanbanCalc.Location = flow.LocationTo;
                                //if (tiao == 0)
                                //{
                                //    string[] tiaoArray = flowStrategy.QiTiaoBian.Split('-');
                                //    if (tiaoArray.Count() == 3)
                                //    {
                                //        tiao = Convert.ToInt32(tiaoArray[1]);
                                //    }
                                //}
                                kanbanCalc.Tiao = tiao;
                                kanbanCalc.Bian = Convert.ToInt32(flowStrategy.LeadTime);

                                LocationBinItem locationBinItem = locationBinItemList.Where(l => l.Location == flow.LocationTo && l.Item == flowDetail.Item).FirstOrDefault();
                                if (locationBinItem != null)
                                {
                                    kanbanCalc.LocBin = locationBinItem.Bin;
                                }
                                // kanbanCalc.Shelf = flowDetail.Shelf;
                                kanbanCalc.LastUseDate = DateTime.Parse("1900-1-1 00:00:00");
                                //if (!calcRegion.IsAutoExportKanBanCard)
                                //{
                                //    if (freezeDate == DateTime.Parse("2999-1-1 00:00:00") && kanbanCalc.KanbanDeltaNum < 0)
                                //    {
                                //        freezeDate = GetFreezeDate(startDate, flow, Convert.ToInt32(flowStrategy.LeadTime));
                                //    }
                                //    //  kanbanCalc.FreezeDate = freezeDate;
                                //    if (effDate == DateTime.Parse("1900-1-1 00:00:00") && kanbanCalc.KanbanDeltaNum > 0)
                                //    {
                                //        effDate = GetEffDate(startDate, flow, Convert.ToInt32(flowStrategy.LeadTime));
                                //    }
                                //}
                                //else
                                //{
                                if (freezeDate == DateTime.Parse("2999-1-1 00:00:00") && kanbanCalc.KanbanDeltaNum <= 0)
                                {
                                    freezeDate = dateTimeNow;
                                }
                                if (effDate == DateTime.Parse("1900-1-1 00:00:00") && kanbanCalc.KanbanDeltaNum >= 0)
                                {
                                    effDate = dateTimeNow;
                                }
                                //}
                                kanbanCalc.FreezeDate = freezeDate;
                                kanbanCalc.EffectiveDate = effDate;
                                kanbanCalc.OpTime = DateTime.Parse("1900-1-1 00:00:00");
                                kanbanCalc.BatchNo = batchno;

                                #region 处理双轨
                                if (!string.IsNullOrEmpty(groupNo))
                                {
                                    IList<KanbanCalc> kCalcList = kanbanCalcList.Where(k => k.MultiSupplyGroup == groupNo & k.Region == calcRegion.Code).ToList();

                                    if (kCalcList != null && kCalcList.Count > 0)
                                    {
                                        decimal gKanbanNum = kCalcList.Max(k => k.CalcKanbanNum);
                                        if (gKanbanNum > calculatedKanbanNum)
                                        {
                                            kanbanCalc.CalcKanbanNum = gKanbanNum;
                                            kanbanCalc.KanbanDeltaNum = (int)Math.Ceiling(gKanbanNum) - existKanbanNum;
                                        }
                                        else
                                        {
                                            foreach (KanbanCalc k in kCalcList)
                                            {
                                                k.CalcKanbanNum = calculatedKanbanNum;
                                                k.KanbanDeltaNum = (int)Math.Ceiling(calculatedKanbanNum) - existKanbanNum;
                                            }
                                        }
                                    }

                                }

                                kanbanCalcList.Add(kanbanCalc);
                                #endregion

                                #endregion

                                #region 记录路线明细上的日产量,交货趟次
                                flowDetail.MaxStock = kanbanCalc.CalcConsumeQty;
                                flowDetail.MrpWeight = kanbanCalc.Tiao;
                                genericMgr.Update(flowDetail);
                                #endregion

                            }
                            #endregion
                        }
                        #endregion

                        #region 生产
                        if (kbCalc == (int)com.Sconit.CodeMaster.KBCalculation.CatItem)
                        {
                            IList<FlowDetail> flowDetailList = genericMgr.FindAll<FlowDetail>(" from FlowDetail d where d.Flow = ?", flow.Code);
                            com.Sconit.Entity.SCM.FlowStrategy flowStrategy = genericMgr.FindById<com.Sconit.Entity.SCM.FlowStrategy>(flow.Code);

                            DateTime freezeDate = DateTime.Parse("2999-1-1 00:00:00");
                            DateTime effDate = DateTime.Parse("1900-1-1 00:00:00");

                            foreach (FlowDetail flowDetail in flowDetailList)
                            {
                                IList<ItemDailyConsume> flowDetailItemDailyConsumeList = itemDailyConsumeList.Where(p => p.Item == flowDetail.Item).ToList();
                                // IList<KanbanCard> existKanbanCardList = genericMgr.FindAll<KanbanCard>("from KanbanCard c where c.FlowDetailId = ?", flowDetail.Id);
                                int existKanbanNum = flowDetail.CycloidAmount;

                                decimal calculatedKanbanNum = 0;
                                Decimal calcQty = 0;

                                decimal batchSizeKanbanNum = 0;
                                decimal safeStockKanbanNum = 0;

                                if (flowDetailItemDailyConsumeList != null && flowDetailItemDailyConsumeList.Count > 0)
                                {
                                    if (flowDetail.UnitCount != 0)
                                    {
                                        decimal maxQty = flowDetailItemDailyConsumeList.Max(f => f.Qty);
                                        decimal batchsizeQty = flowDetail.BatchSize.HasValue ? flowDetail.BatchSize.Value : 0;

                                        #region 正常零件
                                        //if (string.IsNullOrEmpty(flowDetail.GroupNo))
                                        //{
                                        calcQty = maxQty;
                                        //}
                                        #endregion

                                        #region 种别件
                                        //else
                                        //{
                                        //    IList<FlowDetail> gFlowDetailList = flowDetailList.Where(f => f.GroupNo == flowDetail.GroupNo).ToList();
                                        //    decimal groupQty = 0;
                                        //    foreach (FlowDetail g in gFlowDetailList)
                                        //    {
                                        //        IList<ItemDailyConsume> l = itemDailyConsumeList.Where(p => p.Item == g.Item).ToList();
                                        //        if (l != null && l.Count > 0)
                                        //        {
                                        //            groupQty += l.Max(f => f.Qty);
                                        //        }
                                        //    }
                                        //    batchsizeQty = batchsizeQty * maxQty / groupQty;
                                        //}
                                        #endregion

                                        #region 计算


                                        decimal safeStock = flowDetail.SafeStock == null ? 0 : flowDetail.SafeStock.Value;
                                        //  decimal shiftType = string.IsNullOrEmpty(flowDetail.Shift) ? 1 : Convert.ToInt32(flowDetail.Shift);
                                        decimal shiftType = 1;
                                        //安全张数, 需求数*安全时数/(8*焊装班次)
                                        if (flowDetail.UnitCount < 2 * maxQty)
                                        {
                                            int safeQty = (int)Math.Round(maxQty * safeStock / (8 * shiftType), 0, MidpointRounding.AwayFromZero);
                                            safeStockKanbanNum = (int)Math.Round(safeQty / flowDetail.UnitCount, 0, MidpointRounding.AwayFromZero);
                                        }
                                        //批量张数
                                        batchSizeKanbanNum = (int)Math.Ceiling(batchsizeQty / flowDetail.UnitCount);

                                        calculatedKanbanNum = batchSizeKanbanNum + safeStockKanbanNum;
                                        calcQty = maxQty;
                                        #endregion
                                    }

                                }
                                else
                                {
                                    #region 本次无日用量的,应该所有的都冻结
                                    #endregion
                                }

                                #region 记录计算结果
                                KanbanCalc kanbanCalc = new KanbanCalc();
                                kanbanCalc.Flow = flow.Code;
                                kanbanCalc.KBCalc = (com.Sconit.CodeMaster.KBCalculation)kbCalc;
                                kanbanCalc.CalcConsumeQty = calcQty;
                                kanbanCalc.CalcKanbanNum = calculatedKanbanNum;

                                kanbanCalc.Flow = flow.Code;
                                kanbanCalc.FlowDetailId = flowDetail.Id;
                                kanbanCalc.Item = flowDetail.Item;

                                Item item = itemList.Where(k => k.Code == kanbanCalc.Item).FirstOrDefault();
                                kanbanCalc.ItemDescription = item.Description;
                                kanbanCalc.Container = item.Description;
                                if (item.IsKit)
                                {
                                    kanbanCalc.IsKit = item.IsKit;
                                    kanbanCalc.KitCount = itemKitList.Where(p => p.KitItem == item.Code).Count();
                                }
                                ItemTrace itemTrace = itemTraceList.Where(t => t.Item == flowDetail.Item).FirstOrDefault();
                                if (itemTrace != null)
                                {
                                    kanbanCalc.IsTrace = true;
                                }
                                //kanbanCalc.GroupNo = flowDetail.GroupNo;
                                //kanbanCalc.GroupDesc = flowDetail.GroupDesc;
                                kanbanCalc.KanbanNum = existKanbanNum;
                                kanbanCalc.Qty = flowDetail.UnitCount;
                                kanbanCalc.KanbanDeltaNum = (int)Math.Ceiling(calculatedKanbanNum) - existKanbanNum;
                                kanbanCalc.Region = calcRegion.Code;
                                kanbanCalc.RegionName = calcRegion.Name;
                                kanbanCalc.Supplier = flow.PartyFrom;
                                var sup = partyList.Where(p => p.Code == flow.PartyFrom).FirstOrDefault();
                                kanbanCalc.SupplierName = sup.Name;
                                if (sup is Supplier)
                                {
                                    Supplier su = sup as Supplier;
                                    kanbanCalc.SupplierName = string.IsNullOrEmpty(su.ShortCode) ? su.Name.Substring(0, 4) : su.ShortCode;
                                }

                                kanbanCalc.LastUseDate = DateTime.Parse("1900-1-1 00:00:00");
                                LocationBinItem locationBinItem = locationBinItemList.Where(l => l.Location == flow.LocationTo && l.Item == flowDetail.Item).FirstOrDefault();
                                if (locationBinItem != null)
                                {
                                    kanbanCalc.LocBin = locationBinItem.Bin;
                                }
                                kanbanCalc.BatchNo = batchno;
                                kanbanCalc.BatchKanbanNum = batchSizeKanbanNum;
                                kanbanCalc.SafeKanbanNum = safeStockKanbanNum;
                                kanbanCalc.Location = flow.LocationTo;
                                if (kanbanCalc.KanbanDeltaNum > 0)
                                {
                                    effDate = DateTime.Now;
                                }
                                if (kanbanCalc.KanbanDeltaNum < 0)
                                {
                                    freezeDate = DateTime.Now;
                                }
                                kanbanCalc.EffectiveDate = effDate;
                                kanbanCalc.FreezeDate = freezeDate;

                                kanbanCalcList.Add(kanbanCalc);

                                #endregion
                            }

                        }
                        #endregion
                    }
                }
            }

            #region 生成结果
            // kanbanCalcList.AddRange(lerror);
            if (kanbanCalcList.Count > 0)
            {
                foreach (KanbanCalc kc in kanbanCalcList)
                {
                    this.genericMgr.Create(kc);
                }
            }
            #endregion
        }
Example #2
0
        public void ImportKanbanCalc2(Stream inputStream, string batchno, User currentUser)
        {

            if (inputStream.Length == 0)
            {
                throw new BusinessException("Import.Stream.Empty");
            }
            BusinessException businessException = new BusinessException();
            HSSFWorkbook workbook = new HSSFWorkbook(inputStream);

            ISheet sheet = workbook.GetSheetAt(0);
            IEnumerator rows = sheet.GetRowEnumerator();
            IDictionary<string, KanbanCalc> importedKanbanCalcs = new Dictionary<string, KanbanCalc>();
            ImportHelper.JumpRows(rows, 1);

            #region 列定义
            int colFlowDet = 0;//路线明细
            int colKanbanDeltaNum = 26;//变化张数
            #endregion
            int i = 1;
            while (rows.MoveNext())
            {
                i++;
                HSSFRow row = (HSSFRow)rows.Current;
                if (!ImportHelper.CheckValidDataRow(row, 1, 26))
                {
                    break;//边界
                }
                int flowDetId = 0;
                int kanbanDeltaNum = 0;
                #region 路线明细Id
                try
                {
                    try
                    {
                        flowDetId = Convert.ToInt32(row.GetCell(colFlowDet).NumericCellValue);
                    }
                    catch (Exception)
                    {
                        flowDetId = Convert.ToInt32(row.GetCell(colFlowDet).ToString());
                    }
                }
                catch
                {
                    businessException.AddMessage("无法获取导入数据的路线ID,第{0}行。\t", i.ToString());
                    continue;
                }
                #endregion

                #region 变化张数
                try
                {
                    try
                    {
                        kanbanDeltaNum = Convert.ToInt32(row.GetCell(colKanbanDeltaNum).NumericCellValue);
                    }
                    catch (Exception)
                    {
                        kanbanDeltaNum = Convert.ToInt32(row.GetCell(colKanbanDeltaNum).ToString());
                    }
                }
                catch
                {
                    businessException.AddMessage("无法获取导入数据差异,第{0}行。\t", i.ToString());
                    continue;
                }
                #endregion

                if (importedKanbanCalcs.ContainsKey(flowDetId.ToString()))
                {
                    businessException.AddMessage("Import.Stream.DuplicateRow, id为{0}", flowDetId.ToString());
                }
                else
                {
                    KanbanCalc calc = new KanbanCalc();
                    calc.BatchNo = batchno;
                    calc.FlowDetailId = flowDetId;
                    calc.KanbanDeltaNum = kanbanDeltaNum;
                    importedKanbanCalcs.Add(flowDetId.ToString(), calc);
                }
            }
            if (businessException.HasMessage)
            {
                throw businessException;
            }
            this.UpdateBatch(batchno, currentUser, importedKanbanCalcs);
        }