Inheritance: CustomerScheduleDetailBase
 public virtual void UpdateCustomerScheduleDetail(CustomerScheduleDetail entity)
 {
     entityDao.UpdateCustomerScheduleDetail(entity);
 }
 public virtual void DeleteCustomerScheduleDetail(CustomerScheduleDetail entity)
 {
     entityDao.DeleteCustomerScheduleDetail(entity);
 }
Example #3
0
        public IList<CustomerSchedule> ReadCustomerScheduleFromXls(Stream inputStream, User user, DateTime? startDate, DateTime? endDate,
            string flowCode, string refScheduleNo, bool isItemRef)
        {
            if (inputStream.Length == 0)
            {
                throw new BusinessErrorException("Import.Stream.Empty");
            }

            HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
            Sheet sheet = workbook.GetSheetAt(0);
            IEnumerator rows = sheet.GetRowEnumerator();

            #region 读取路线,参考日程号等
            Flow flow = null;
            decimal leadTime = 0M;
            IList<string> flowList = new List<string>();
            if (flowCode != null && flowCode.Trim() != string.Empty)
            {
                flow = flowMgr.CheckAndLoadFlow(flowCode, true, true);
                leadTime = flow.LeadTime.HasValue ? flow.LeadTime.Value : 0M;
                flowList.Add(flowCode);
            }

            Row typeRow = sheet.GetRow(5);
            Row dateRow = sheet.GetRow(6);

            //IList<CustomerSchedule> customerSchedules = customerScheduleMgr.GetCustomerSchedules(flowCode, refScheduleNo, null, null, null);
            //if (customerSchedules.Count > 0)
            //{
            //    throw new BusinessErrorException("MRP.Schedule.Import.CannotImportSameProductionSchedule");
            //}
            #endregion

            #region CustomerSchedule

            IList<CustomerScheduleDetail> customerScheduleDetaiList = new List<CustomerScheduleDetail>();
            #endregion

            ImportHelper.JumpRows(rows, 7);

            #region 列定义
            int colItemCode = 0;//物料代码或参考物料号
            // int colItemDescription = 1;//物料描述
            int colUom = 2;//单位
            int colUc = 3;//单包装
            #endregion

            while (rows.MoveNext())
            {
                Item item = null;
                Uom uom = null;
                decimal? uc = null;
                string itemReference = null;
                string location = null;
                Flow currentFlow = flow;
                string bom = null;

                Row row = (HSSFRow)rows.Current;
                if (!this.CheckValidDataRow(row, 0, 3))
                {
                    break;//边界
                }
                string rowIndex = (row.RowNum + 1).ToString();

                #region 读取物料代码
                try
                {
                    string itemCode = GetCellStringValue(row.GetCell(colItemCode));
                    if (itemCode == null)
                    {
                        throw new BusinessErrorException("MRP.Schedule.Import.ItemCode.Empty", rowIndex);
                    }
                    item = itemMgr.LoadItem(itemCode);
                    if (item == null)
                    {
                        throw new BusinessErrorException("MRP.Schedule.Import.Item.NotExist", rowIndex);
                    }

                    #region 找路线,先找生产,在找销售,找不到就悲剧
                    DetachedCriteria criteria = DetachedCriteria.For(typeof(FlowDetail));
                    criteria.CreateAlias("Flow", "f");
                    criteria.Add(Expression.Eq("f.IsActive", true));
                    criteria.Add(Expression.Or(
                        Expression.And(Expression.Eq("f.FlowStrategy", BusinessConstants.CODE_MASTER_FLOW_STRATEGY_VALUE_MRP),
                        Expression.Eq("f.Type", BusinessConstants.CODE_MASTER_FLOW_TYPE_VALUE_PRODUCTION)),
                        Expression.Eq("f.Type", BusinessConstants.CODE_MASTER_FLOW_TYPE_VALUE_DISTRIBUTION)));
                    // criteria.Add(Expression.Eq("f.FlowStrategy", BusinessConstants.CODE_MASTER_FLOW_STRATEGY_VALUE_MRP));
                    criteria.Add(Expression.Eq("Item.Code", item.Code));
                    criteria.AddOrder(Order.Desc("f.Type"));
                    if (flow != null)
                    {
                        criteria.Add(Expression.Eq("f.Code", flow.Code));
                    }
                    IList<FlowDetail> flowDetailList = criteriaMgr.FindAll<FlowDetail>(criteria);
                    if (flowDetailList == null || flowDetailList.Count == 0)
                    {
                        throw new BusinessErrorException("MRP.Schedule.Import.Flow.NotExist", rowIndex);
                    }

                    currentFlow = flowDetailList[0].Flow;
                    location = flowDetailList[0].DefaultLocationFrom.Code;
                    leadTime = currentFlow.LeadTime.HasValue ? currentFlow.LeadTime.Value : 0M;
                    if (currentFlow.Type == BusinessConstants.CODE_MASTER_FLOW_TYPE_VALUE_PRODUCTION)
                    {
                        bom = flowDetailList[0].Bom != null ? flowDetailList[0].Bom.Code : null;
                    }
                    if (!flowList.Contains(currentFlow.Code))
                    {
                        flowList.Add(currentFlow.Code);
                    }
                    #endregion

                }
                catch (BusinessErrorException ex)
                {
                    throw ex;
                }
                #endregion

                #region 读取单位
                try
                {
                    string uomCode = GetCellStringValue(row.GetCell(colUom));
                    if (uomCode != null)
                    {
                        uom = uomMgr.CheckAndLoadUom(uomCode);
                    }
                }
                catch
                {
                    this.ThrowCommonError(row, colUom);
                }
                #endregion

                #region 读取单包装
                try
                {
                    string uc_ = GetCellStringValue(row.GetCell(colUc));
                    if (uc_ != null)
                    {
                        uc = Convert.ToDecimal(uc_);
                    }
                }
                catch
                {
                    this.ThrowCommonError(row, colUc);
                }
                #endregion

                #region 读取数量
                try
                {
                    for (int i = 4; ; i++)
                    {
                        string periodType = GetCellStringValue(typeRow.GetCell(i));

                        if (periodType != BusinessConstants.CODE_MASTER_TIME_PERIOD_TYPE_VALUE_DAY &&
                            periodType != BusinessConstants.CODE_MASTER_TIME_PERIOD_TYPE_VALUE_MONTH &&
                            periodType != BusinessConstants.CODE_MASTER_TIME_PERIOD_TYPE_VALUE_WEEK)
                        {
                            break;
                        }
                        Cell dateCell = dateRow.GetCell(i);
                        DateTime? dateCellValue = null;
                        if (dateCell != null && dateCell.CellType == CellType.NUMERIC)
                        {
                            dateCellValue = dateCell.DateCellValue;
                        }
                        else
                        {
                            break;
                        }
                        if (startDate.HasValue && dateCellValue.Value.Date < startDate.Value.Date)
                        {
                            continue;
                        }
                        if (endDate.HasValue && dateCellValue.Value.Date > endDate.Value.Date)
                        {
                            break;
                        }
                        string qtyValue = GetCellStringValue(row.GetCell(i));
                        decimal qty = 0M;
                        if (qtyValue != null)
                        {
                            qty = Convert.ToDecimal(qtyValue);
                        }
                        if (qty < 0M)
                        {
                            throw new BusinessErrorException("MRP.Schedule.Import.Qty.MustGreatThanZero", rowIndex);
                        }
                        else
                        {
                            CustomerScheduleDetail customerScheduleDetail = new CustomerScheduleDetail();
                            customerScheduleDetail.DateFrom = DateTimeHelper.GetStartTime(periodType, dateCellValue.Value);
                            customerScheduleDetail.DateTo = DateTimeHelper.GetEndTime(periodType, dateCellValue.Value);
                            customerScheduleDetail.Item = item.Code;
                            customerScheduleDetail.ItemDescription = item.Description;
                            customerScheduleDetail.ItemReference = itemReference;
                            customerScheduleDetail.Location = location;
                            customerScheduleDetail.Type = periodType;
                            customerScheduleDetail.UnitCount = uc.Value;
                            customerScheduleDetail.Uom = uom.Code;
                            customerScheduleDetail.StartTime = customerScheduleDetail.DateFrom.AddHours(-(double)leadTime);
                            customerScheduleDetail.Qty = qty;
                            customerScheduleDetail.Flow = currentFlow.Code;
                            if (currentFlow.Type == BusinessConstants.CODE_MASTER_FLOW_TYPE_VALUE_PRODUCTION)
                            {
                                customerScheduleDetail.Bom = bom == null ? item.Code : bom;
                            }
                            customerScheduleDetaiList.Add(customerScheduleDetail);
                        }
                    }
                }
                catch (Exception)
                {
                    throw new BusinessErrorException("MRP.Schedule.Import.Qty.Error", rowIndex);
                }
                #endregion
            }

            #region 建多个客户需求计划
            IList<CustomerSchedule> customerScheduleList = new List<CustomerSchedule>();
            if (flowList.Count > 0)
            {
                foreach (string f in flowList)
                {
                    CustomerSchedule cs = new CustomerSchedule();
                    cs.Flow = f;
                    cs.ReferenceScheduleNo = refScheduleNo;

                    var q = customerScheduleDetaiList.Where<CustomerScheduleDetail>(p => p.Flow == f);

                    var gCustomerScheduleDetails = from c in q.ToList()
                                                   group c by new
                                                   {
                                                       c.DateFrom,
                                                       c.DateTo,
                                                       c.Item,
                                                       c.ItemDescription,
                                                       c.ItemReference,
                                                       c.Location,
                                                       c.Type,
                                                       c.UnitCount,
                                                       c.Uom,
                                                       c.StartTime,
                                                       c.Flow,
                                                       c.Bom
                                                   } into customerScheduleDetail
                                                   select new CustomerScheduleDetail
                                        {

                                            DateFrom = customerScheduleDetail.Key.DateFrom,
                                            DateTo = customerScheduleDetail.Key.DateTo,
                                            Item = customerScheduleDetail.Key.Item,
                                            ItemDescription = customerScheduleDetail.Key.ItemDescription,
                                            ItemReference = customerScheduleDetail.Key.ItemReference,
                                            Location = customerScheduleDetail.Key.Location,
                                            Type = customerScheduleDetail.Key.Type,
                                            UnitCount = customerScheduleDetail.Key.UnitCount,
                                            Uom = customerScheduleDetail.Key.Uom,
                                            StartTime = customerScheduleDetail.Key.StartTime,
                                            Qty = customerScheduleDetail.Sum(p => p.Qty),
                                            Flow = customerScheduleDetail.Key.Flow,
                                            Bom = customerScheduleDetail.Key.Bom
                                        };
                    cs.CustomerScheduleDetails = gCustomerScheduleDetails.OrderBy(c => c.StartTime).ToList<CustomerScheduleDetail>();
                    customerScheduleList.Add(cs);
                }
            }

            #endregion

            return customerScheduleList;
        }
 public virtual void CreateCustomerScheduleDetail(CustomerScheduleDetail entity)
 {
     Create(entity);
 }
Example #5
0
        public void TransformationPlan(User user)
        {
            try
            {
                DateTime datetimeNow = System.DateTime.Now;
                #region  �����ܼƻ�
                string weeklySql = " select t from TEMP_FORD_EDI_830 as t where t.IsHandle=0 order by t.Id asc";
                IList<TEMP_FORD_EDI_830> weeklyPlans = this.genericMgr.FindAllWithCustomQuery<TEMP_FORD_EDI_830>(weeklySql);
                IList<EDIFordPlan> planList = new List<EDIFordPlan>();
                if (weeklyPlans != null && weeklyPlans.Count > 0)
                {
                    //IList<FlowDetail> flowdets = this.genericMgr.FindAllWithCustomQuery<FlowDetail>(string.Format(" select d from FlowDetail as d where  d.ReferenceItemCode in('{0}') ",string.Join("','", weeklyPlans.Select(w => w.Part_Num).Distinct().ToArray())));
                    //IList<ItemReference> itemReferences = this.genericMgr.FindAllWithCustomQuery<ItemReference>(string.Format(" select t from ItemReference as t where t.ReferenceCode in('{0}')", string.Join("','", weeklyPlans.Select(w => w.Part_Num).Distinct().ToArray())));
                    foreach (var weekly in weeklyPlans)
                    {
                        #region
                        //var flowDet = flowdets.Where(f => f.ReferenceItemCode == weekly.Part_Num);
                        //if (flowDet == null || flowDet.Count() == 0)
                        //{
                        //    throw new Exception("�������Ϻ��Ҳ�����Ӧ����ϸ��");
                        //}
                        //ItemReference itemReference = itemReferences.Where(i => i.ReferenceCode == weekly.Part_Num).First();
                        EDIFordPlan eDIFordPlan = new EDIFordPlan();
                        eDIFordPlan.TempId = weekly.Id;
                        eDIFordPlan.BatchNo = weekly.BatchNo;
                        eDIFordPlan.Control_Num = weekly.Interchange_Control_Num;
                        eDIFordPlan.SupplierCode = weekly.Ship_From_GSDB_Code;
                        eDIFordPlan.CustomerCode = weekly.Ship_To_GSDB_Code;
                        eDIFordPlan.ReleaseIssueDate =  DateTime.Parse(weekly.Message_Release_Date.Substring(0, 4) + "-" + weekly.Message_Release_Date.Substring(4, 2) + "-" + weekly.Message_Release_Date.Substring(6, 2));
                        eDIFordPlan.Item = string.Empty;
                        eDIFordPlan.ItemDesc = string.Empty;
                        eDIFordPlan.RefItem = weekly.Part_Num;
                        eDIFordPlan.Uom = weekly.UOM;
                        eDIFordPlan.LastShippedQuantity = string.IsNullOrEmpty(weekly.Last_Shipped_Qty) ? null : (decimal?)(Convert.ToDecimal(weekly.Last_Shipped_Qty));
                        eDIFordPlan.LastShippedCumulative = string.IsNullOrEmpty(weekly.Cum_Shipped_Qty) ? null : (decimal?)(Convert.ToDecimal(weekly.Cum_Shipped_Qty));
                        eDIFordPlan.LastShippedDate = string.IsNullOrEmpty(weekly.Last_Shipped_Date) ? null : (DateTime?)(DateTime.Parse(weekly.Last_Shipped_Date.Substring(0, 4) + "-" + weekly.Last_Shipped_Date.Substring(4, 2) + "-" + weekly.Last_Shipped_Date.Substring(6, 2)));
                        eDIFordPlan.DockCode = weekly.Dock_Code;
                        eDIFordPlan.LineFeed = weekly.Line_Feed;
                        eDIFordPlan.StorageLocation = weekly.Reserve_Line_Feed;
                        eDIFordPlan.IntermediateConsignee = weekly.Intermediate_Consignee;
                        eDIFordPlan.ForecastQty = string.IsNullOrEmpty(weekly.Forecast_Net_Qty) ? 0 : Convert.ToDecimal(weekly.Forecast_Net_Qty);
                        eDIFordPlan.ForecastCumQty = string.IsNullOrEmpty(weekly.Forecast_Cum_Qty) ? 0 : Convert.ToDecimal(weekly.Forecast_Cum_Qty);
                        eDIFordPlan.ForecastDate = string.IsNullOrEmpty(weekly.Forecast_Date) ? System.DateTime.Now.AddDays(-365) : DateTime.Parse(weekly.Forecast_Date.Substring(0, 4) + "-" + weekly.Forecast_Date.Substring(4, 2) + "-" + weekly.Forecast_Date.Substring(6, 2));
                        eDIFordPlan.CreateDate = datetimeNow;
                        eDIFordPlan.CreateUserName = user.Name;
                        eDIFordPlan.Type = weekly.Forecast_Date_Qual_r;
                        eDIFordPlan.PurchaseOrder = weekly.Purchase_Order_Num;
                        this.genericMgr.Create(eDIFordPlan);
                        planList.Add(eDIFordPlan);
                        //weekly.IsHandle = true    ;
                        //this.genericMgr.Update(weekly);
                        #endregion
                    }
                    string upIsHandleSql = string.Format("update TEMP_FORD_EDI_830 set IsHandle=1 where Interchange_Control_Num in ('{0}')",string.Join("','", weeklyPlans.Select(w=>w.Interchange_Control_Num).Distinct().ToArray()));
                    this.genericMgr.ExecuteSql(upIsHandleSql);

                    #region �ܼƻ�ת�ͻ�����
                    var groupWeeklys = weeklyPlans.GroupBy(w => w.Interchange_Control_Num).ToDictionary(d => d.Key, d => d.OrderBy(rd => rd.Forecast_Date).ToList());

                    foreach (var g in groupWeeklys)
                    {
                        string searchFlowCodeSql = string.Format("select Code from flowmstr where type='Distribution' and CustomerCodes like '%{0}%' and SupplierCodes like '%{1}%'", g.Value.First().Ship_To_GSDB_Code, g.Value.First().Ship_From_GSDB_Code);
                        var flowCodes = this.genericMgr.GetDatasetBySql(searchFlowCodeSql).Tables[0];
                        string flowCode = string.Empty;
                        foreach (System.Data.DataRow row in flowCodes.Rows)
                        {
                            flowCode = row[0].ToString();
                        }
                        foreach (var v in g.Value)
                        {
                            v.TempFlow = flowCode;
                        }
                    }
                    var groupWeeklyByFlow = weeklyPlans.GroupBy(w => w.TempFlow).ToDictionary(d => d.Key, d => d.OrderBy(rd => rd.Forecast_Date).ToList());
                    IList<CustomerScheduleDetail> tempDetList = new List<CustomerScheduleDetail>();
                    foreach (var g in groupWeeklyByFlow)
                    {
                        Flow currentFlow = this.flowMgr.LoadFlow(g.Key);
                        if (currentFlow == null) continue;
                        int customerPlanVersion = numberControlMgr.GenerateNumberNextSequence("CustomerPlan_" + currentFlow.Code + "_" + BusinessConstants.CODE_MASTER_TIME_PERIOD_TYPE_VALUE_WEEK);
                        var mrpLeadtime = currentFlow.MrpLeadTime.HasValue ? Convert.ToDouble(-currentFlow.MrpLeadTime.Value) : 0;
                        CustomerSchedule mstr = new CustomerSchedule
                        {
                            ReferenceScheduleNo = g.Key + "-Weekly-" + customerPlanVersion.ToString().PadLeft(3,'0'),
                            Flow = g.Key,
                            Status = BusinessConstants.CODE_MASTER_STATUS_VALUE_SUBMIT,
                            Type = BusinessConstants.CODE_MASTER_TIME_PERIOD_TYPE_VALUE_WEEK,
                            CreateDate = datetimeNow,
                            CreateUser = user.Code,
                            LastModifyDate = datetimeNow,
                            LastModifyUser = user.Code,
                            ReleaseDate=  datetimeNow,
                            ReleaseUser=user.Code,
                            Version = customerPlanVersion,
                            ShipFlow = currentFlow.ShipFlow,
                        };
                        this.genericMgr.Create(mstr);
                        for (int i = 0; i < g.Value.Count; i++)
                        {
                            var r = g.Value[i];
                            var rn = g.Value[i + 1];
                            if (rn.Forecast_Date_Qual_r == "M")
                            {
                                for (int ii = i; ii < g.Value.Count; ii++)
                                {
                                    var r1 = g.Value[ii];
                                    var sDate = DateTime.Parse(r1.Forecast_Date.Substring(0, 4) + "-" + r1.Forecast_Date.Substring(4, 2) + "-" + r1.Forecast_Date.Substring(6, 2));
                                    var firstDate = DateTime.Parse(r1.Forecast_Date.Substring(0, 4) + "-" + r1.Forecast_Date.Substring(4, 2) + "-" + r1.Forecast_Date.Substring(6, 2));
                                    var year = sDate.Date.Year;
                                    var month = sDate.Date.Month;
                                    if (sDate.Date.Month == 12)
                                    {
                                        year = sDate.Date.Year + 1;
                                        month = 0;
                                    }
                                    var eDate = new DateTime(year, month + 1, 1).AddDays(-1);
                                    TimeSpan ts = eDate - sDate;
                                    int sub = ts.Days;
                                    int surplusWeekly = sub % 7 > 4 ? sub / 7 + 1 : sub / 7;
                                    if (surplusWeekly == 0)
                                    {
                                        if (sDate.Date.Month == 11)
                                        {
                                            year = sDate.Date.Year + 1;
                                            month = -1;
                                        }
                                        else if (sDate.Date.Month == 12)
                                        {
                                            year = sDate.Date.Year + 1;
                                            month = 0;
                                        }
                                        eDate = new DateTime(year, month + 2, 1).AddDays(-1);
                                        ts = eDate - sDate;
                                        sub = ts.Days;
                                        surplusWeekly = sub % 7 > 4 ? sub / 7 + 1 : sub / 7;
                                    }
                                    var averageQty = Convert.ToDecimal(r.Forecast_Net_Qty) / surplusWeekly;
                                    for (int j = 0; j < surplusWeekly; j++)
                                    {
                                        var cFlowDets = currentFlow.FlowDetails.Where(d => d.ReferenceItemCode == r1.Part_Num);
                                        FlowDetail fdet = cFlowDets != null && cFlowDets.Count() > 0 ? cFlowDets.First() : new FlowDetail();
                                        if (fdet.Item != null)
                                        {
                                            #region
                                            CustomerScheduleDetail cdet = new CustomerScheduleDetail();
                                            cdet.CustomerSchedule = mstr;
                                            cdet.Item = fdet.Item.Code;
                                            cdet.ItemDescription = fdet.Item.Description;
                                            cdet.ItemReference = r1.Part_Num;
                                            cdet.Type = BusinessConstants.CODE_MASTER_TIME_PERIOD_TYPE_VALUE_WEEK;
                                            cdet.Version = mstr.Version;
                                            cdet.DateFrom = sDate;
                                            cdet.DateTo = sDate.AddDays(7);
                                            cdet.Uom = fdet.Uom.Code;
                                            cdet.UnitCount = fdet.UnitCount;
                                            cdet.Qty = averageQty < 0 ? 0 : averageQty;
                                            cdet.Location = currentFlow.LocationFrom != null ? currentFlow.LocationFrom.Code : string.Empty;
                                            cdet.StartTime = sDate.AddDays(-mrpLeadtime).Date;
                                            cdet.Flow = mstr.Flow;
                                            cdet.ReferenceScheduleNo = mstr.ReferenceScheduleNo;
                                            cdet.ShipFlow = mstr.ShipFlow;
                                            cdet.ShipQty = 0;
                                            cdet.FordPlanId = planList.FirstOrDefault(d => d.RefItem == r1.Part_Num && d.Control_Num == r1.Interchange_Control_Num && d.ForecastDate == firstDate).Id;
                                            //this.genericMgr.Create(cdet);
                                            tempDetList.Add(cdet);
                                            sDate = sDate.AddDays(7);
                                            #endregion
                                        }
                                    }
                                }
                                break;
                            }
                            var currentFlowDets = currentFlow.FlowDetails.Where(d => d.ReferenceItemCode == r.Part_Num);
                            FlowDetail flowdet = currentFlowDets != null && currentFlowDets.Count() > 0 ? currentFlowDets.First() : new FlowDetail();
                            if (flowdet.Item != null)
                            {
                                #region
                                CustomerScheduleDetail det = new CustomerScheduleDetail();
                                det.CustomerSchedule = mstr;
                                det.Item = flowdet.Item != null ? flowdet.Item.Code : string.Empty;
                                det.ItemDescription = flowdet.Item != null ? flowdet.Item.Description : string.Empty;
                                det.ItemReference = r.Part_Num;
                                //det.Bom = null;
                                det.Type = BusinessConstants.CODE_MASTER_TIME_PERIOD_TYPE_VALUE_WEEK;
                                det.DateFrom = string.IsNullOrEmpty(r.Forecast_Date) ? System.DateTime.Now.AddDays(-365) : DateTime.Parse(r.Forecast_Date.Substring(0, 4) + "-" + r.Forecast_Date.Substring(4, 2) + "-" + r.Forecast_Date.Substring(6, 2));
                                det.DateTo = det.DateFrom.AddDays(7);
                                det.Uom = flowdet.Uom.Code;
                                det.UnitCount = flowdet.UnitCount;
                                det.Qty = string.IsNullOrEmpty(r.Forecast_Net_Qty) ? 0 : Convert.ToDecimal(r.Forecast_Net_Qty) < 0 ? 0 : Convert.ToDecimal(r.Forecast_Net_Qty);
                                det.Location = currentFlow.LocationFrom != null ? currentFlow.LocationFrom.Code : string.Empty;
                                //det.StartTime = det.DateFrom;
                                det.StartTime = det.DateFrom.AddDays(-mrpLeadtime).Date;
                                det.Version = mstr.Version;
                                det.Flow = mstr.Flow;
                                det.ReferenceScheduleNo = mstr.ReferenceScheduleNo;
                                det.ShipFlow = mstr.ShipFlow;
                                det.ShipQty = 0;
                                det.FordPlanId = planList.FirstOrDefault(d => d.RefItem == r.Part_Num && d.Control_Num == r.Interchange_Control_Num && d.ForecastDate==det.DateFrom).Id;
                                //this.genericMgr.Create(det);
                                tempDetList.Add(det);
                                #endregion
                            }
                        }
                        if(tempDetList.Count>0){
                            var groupbyItem = tempDetList.GroupBy(d => new { d.Item, d.DateFrom });
                            foreach (var gi in groupbyItem)
                            {
                                var det = gi.First();
                                det.Qty = gi.Sum(gd => gd.Qty);
                                this.genericMgr.Create(det);
                            }
                            tempDetList.Clear();
                        }

                    }
                    #endregion
                }
                #endregion

                #region  ������ƻ�
                planList.Clear();
                //planList = new List<EDIFordPlan>();
                string dailySql = " select t from TEMP_FORD_EDI_862 as t where t.IsHandle=0 order by t.Id asc";
                IList<TEMP_FORD_EDI_862> dailyPlans = this.genericMgr.FindAllWithCustomQuery<TEMP_FORD_EDI_862>(dailySql);
                if (dailyPlans != null && dailyPlans.Count > 0)
                {
                    //IList<FlowDetail> flowdets = this.genericMgr.FindAllWithCustomQuery<FlowDetail>(string.Format(" select d from FlowDetail as d where exists( select 1 from Flow as m where m.Code=d.Flow and m.Description like '%����%' and m.IsActive=1) and d.ReferenceItemCode in('{0}') ", string.Join("','", dailyPlans.Select(w => w.Part_Num).Distinct().ToArray())));
                    //IList<FlowDetail> flowdets = this.genericMgr.FindAllWithCustomQuery<FlowDetail>(string.Format(" select d from FlowDetail as d where d.ReferenceItemCode in('{0}') ", string.Join("','", dailyPlans.Select(w => w.Part_Num).Distinct().ToArray())));
                    //IList<ItemReference> itemReferences = this.genericMgr.FindAllWithCustomQuery<ItemReference>(string.Format(" select t from ItemReference as t where t.ReferenceCode in('{0}')", string.Join("','", dailyPlans.Select(w => w.Part_Num).Distinct().ToArray())));
                    foreach (var daily in dailyPlans)
                    {
                        #region
                        //ItemReference itemReference = itemReferences.Where(i => i.ReferenceCode == daily.Part_Num).First();
                        //var flowDet = flowdets.Where(f => f.ReferenceItemCode == daily.Part_Num);
                        //if (flowDet == null || flowDet.Count() == 0)
                        //{
                        //    throw new Exception("�������Ϻ��Ҳ�����Ӧ����ϸ��");
                        //}
                        EDIFordPlan eDIFordPlan = new EDIFordPlan();
                        eDIFordPlan.TempId = daily.Id;
                        eDIFordPlan.BatchNo = daily.BatchNo;
                        eDIFordPlan.Control_Num = daily.Interchange_Control_Num;
                        eDIFordPlan.SupplierCode = daily.Ship_From_GSDB_Code;
                        eDIFordPlan.CustomerCode = daily.Ship_To_GSDB_Code;
                        eDIFordPlan.ReleaseIssueDate = DateTime.Parse(daily.Message_Release_Date.Substring(0, 4) + "-" + daily.Message_Release_Date.Substring(4, 2) + "-" + daily.Message_Release_Date.Substring(6, 2));
                        //eDIFordPlan.Item = flowDet.First().Item.Code;
                        //eDIFordPlan.ItemDesc = flowDet.First().Item.Desc1;
                        //eDIFordPlan.RefItem = flowDet.First().ReferenceItemCode;
                        eDIFordPlan.Item = string.Empty;
                        eDIFordPlan.ItemDesc = string.Empty;
                        eDIFordPlan.RefItem = daily.Part_Num;
                        eDIFordPlan.Uom = daily.UOM;
                        eDIFordPlan.LastShippedQuantity = string.IsNullOrEmpty(daily.Last_Shipped_Qty) ? null : (decimal?)(Convert.ToDecimal(daily.Last_Shipped_Qty));
                        eDIFordPlan.LastShippedCumulative = string.IsNullOrEmpty(daily.Cum_Shipped_Qty) ? null : (decimal?)(Convert.ToDecimal(daily.Cum_Shipped_Qty));
                        eDIFordPlan.LastShippedDate = string.IsNullOrEmpty(daily.Last_Shipped_Date) ? null : (DateTime?)(DateTime.Parse(daily.Last_Shipped_Date.Substring(0, 4) + "-" + daily.Last_Shipped_Date.Substring(4, 2) + "-" + daily.Last_Shipped_Date.Substring(6, 2)));
                        eDIFordPlan.DockCode = daily.Dock_Code;
                        eDIFordPlan.LineFeed = daily.Line_Feed;
                        eDIFordPlan.StorageLocation = daily.Reserve_Line_Feed;
                        eDIFordPlan.IntermediateConsignee = daily.Intermediate_Consignee;
                        eDIFordPlan.ForecastQty = string.IsNullOrEmpty(daily.Forecast_Net_Qty) ? 0 : Convert.ToDecimal(daily.Forecast_Net_Qty);
                        eDIFordPlan.ForecastCumQty = string.IsNullOrEmpty(daily.Forecast_Cum_Qty) ? 0 : Convert.ToDecimal(daily.Forecast_Cum_Qty);
                        eDIFordPlan.ForecastDate = string.IsNullOrEmpty(daily.Forecast_Date) ? System.DateTime.Now.AddDays(-365) : DateTime.Parse(daily.Forecast_Date.Substring(0, 4) + "-" + daily.Forecast_Date.Substring(4, 2) + "-" + daily.Forecast_Date.Substring(6, 2));
                        eDIFordPlan.CreateDate = datetimeNow;
                        eDIFordPlan.CreateUserName = user.Name;
                        eDIFordPlan.Type = "D";
                        eDIFordPlan.PurchaseOrder = daily.Purchase_Order_Num;
                        this.genericMgr.Create(eDIFordPlan);
                        planList.Add(eDIFordPlan);
                        //daily.IsHandle = true;
                        //this.genericMgr.Update(daily);
                        #endregion
                    }
                    string upIsHandleSql = string.Format("update TEMP_FORD_EDI_862 set IsHandle=1 where Interchange_Control_Num in ('{0}')", string.Join("','", dailyPlans.Select(w => w.Interchange_Control_Num).Distinct().ToArray()));
                    this.genericMgr.ExecuteSql(upIsHandleSql);

                    #region   ��ƻ�ת�ͻ�����
                    var groupDailyPlan = dailyPlans.GroupBy(w => w.Interchange_Control_Num).ToDictionary(d => d.Key, d => d.ToList());

                    foreach (var g in groupDailyPlan)
                    {
                        string searchFlowCodeSql = string.Format("select Code from flowmstr where type='Distribution' and CustomerCodes like '%{0}%' and SupplierCodes like '%{1}%'", g.Value.First().Ship_To_GSDB_Code, g.Value.First().Ship_From_GSDB_Code);
                        var flowCodes = this.genericMgr.GetDatasetBySql(searchFlowCodeSql).Tables[0];
                        string flowCode = string.Empty;
                        foreach (System.Data.DataRow row in flowCodes.Rows)
                        {
                            flowCode = row[0].ToString();
                        }
                        foreach (var v in g.Value)
                        {
                            v.TempFlow = flowCode;
                        }
                    }
                    var groupdailyByFlow = dailyPlans.GroupBy(w => w.TempFlow).ToDictionary(d => d.Key, d => d.OrderBy(rd => rd.Forecast_Date).ToList());

                    IList<CustomerScheduleDetail> tempDetList = new List<CustomerScheduleDetail>();
                    foreach (var g in groupdailyByFlow)
                    {
                        Flow currentFlow = this.flowMgr.LoadFlow(g.Key);
                        if (currentFlow == null) continue;
                        int customerPlanVersion = numberControlMgr.GenerateNumberNextSequence("CustomerPlan_" + g.Key + "_" + BusinessConstants.CODE_MASTER_TIME_PERIOD_TYPE_VALUE_WEEK);
                        var firstWeek = g.Value.First();
                        CustomerSchedule mstr = new CustomerSchedule
                        {
                            ReferenceScheduleNo = g.Key + "-Daily-" + customerPlanVersion.ToString().PadLeft(3,'0'),
                            Flow = g.Key,
                            Status = BusinessConstants.CODE_MASTER_STATUS_VALUE_SUBMIT,
                            Type = BusinessConstants.CODE_MASTER_TIME_PERIOD_TYPE_VALUE_DAY,
                            CreateDate = datetimeNow,
                            CreateUser = user.Code,
                            LastModifyDate = datetimeNow,
                            LastModifyUser = user.Code,
                            ReleaseDate = datetimeNow,
                            ReleaseUser = user.Code,
                            Version = customerPlanVersion,
                            ShipFlow = currentFlow.ShipFlow,
                        };
                        this.genericMgr.Create(mstr);
                        foreach (var r in g.Value)
                        {
                            var currentFlowDets = currentFlow.FlowDetails.Where(d => d.ReferenceItemCode == r.Part_Num);
                            FlowDetail flowdet = currentFlowDets != null && currentFlowDets.Count() > 0 ? currentFlowDets.First() : new FlowDetail();
                            var mrpLeadtime=currentFlow.MrpLeadTime.HasValue?  Convert.ToDouble(-currentFlow.MrpLeadTime.Value):0;
                            if (flowdet.Item != null)
                            {
                                CustomerScheduleDetail det = new CustomerScheduleDetail();
                                det.CustomerSchedule = mstr;
                                det.Item = flowdet.Item != null ? flowdet.Item.Code : string.Empty;
                                det.ItemDescription = flowdet.Item != null ? flowdet.Item.Description : string.Empty;
                                det.ItemReference = r.Part_Num;
                                //det.Bom = null;
                                det.Type = BusinessConstants.CODE_MASTER_TIME_PERIOD_TYPE_VALUE_DAY;
                                det.DateFrom = string.IsNullOrEmpty(r.Forecast_Date) ? System.DateTime.Now.AddDays(-365) : DateTime.Parse(r.Forecast_Date.Substring(0, 4) + "-" + r.Forecast_Date.Substring(4, 2) + "-" + r.Forecast_Date.Substring(6, 2));
                                det.DateTo = string.IsNullOrEmpty(r.Forecast_Date) ? System.DateTime.Now.AddDays(-365) : DateTime.Parse(r.Forecast_Date.Substring(0, 4) + "-" + r.Forecast_Date.Substring(4, 2) + "-" + r.Forecast_Date.Substring(6, 2));
                                det.Uom = flowdet.Uom.Code;
                                det.UnitCount = flowdet.UnitCount;
                                det.Qty = string.IsNullOrEmpty(r.Forecast_Net_Qty) ? 0 : Convert.ToDecimal(r.Forecast_Net_Qty) < 0 ? 0 : Convert.ToDecimal(r.Forecast_Net_Qty);
                                det.Location = currentFlow.LocationFrom != null ? currentFlow.LocationFrom.Code : string.Empty;
                                det.StartTime = det.DateFrom.AddDays(-mrpLeadtime).Date;
                                det.Version = mstr.Version;
                                det.Flow = mstr.Flow;
                                det.ReferenceScheduleNo = mstr.ReferenceScheduleNo;
                                det.ShipFlow = mstr.ShipFlow;
                                det.ShipQty = 0;
                                det.FordPlanId = planList.FirstOrDefault(d => d.RefItem == r.Part_Num && d.Control_Num == r.Interchange_Control_Num && d.ForecastDate==det.DateFrom).Id;
                                //this.genericMgr.Create(det);
                                tempDetList.Add(det);
                            }
                        }
                        if (tempDetList.Count > 0)
                        {
                            var groupbyItem = tempDetList.GroupBy(d => new { d.Item,d.DateFrom});
                            foreach (var gi in groupbyItem)
                            {
                                var det = gi.First();
                                det.Qty = gi.Sum(gd => gd.Qty);
                                this.genericMgr.Create(det);
                            }
                            tempDetList.Clear();
                        }
                    }
                    #endregion
                }
                #endregion
            }
            catch (Exception e)
            {
                throw e;
            }
        }