Пример #1
0
 public int CreatePlanningProductionFromBOM(int bomID, int userId, out Library.DTO.Notification notification)
 {
     notification = new Library.DTO.Notification {
         Type = Library.DTO.NotificationType.Success
     };
     try
     {
         using (PlanningProductionEntities context = CreateContext())
         {
             var planningItem = context.PlanningProduction.Where(o => o.BOMID == bomID).FirstOrDefault();
             if (planningItem == null)
             {
                 context.PlanningProductionMng_function_DeleteIsDeletedItem();
                 //copy data from BOM to Planning Production
                 PlanningProduction             dbPlanning = new PlanningProduction();
                 PlanningProductionMng_BOM_View dbBOM      = context.PlanningProductionMng_BOM_View.Where(o => o.BOMID == bomID).FirstOrDefault();
                 if (dbBOM == null)
                 {
                     throw new Exception("Can not find BOM");
                 }
                 AutoMapper.Mapper.Map <PlanningProductionMng_BOM_View, PlanningProduction>(dbBOM, dbPlanning);
                 context.PlanningProduction.Add(dbPlanning);
                 context.SaveChanges();
                 return(dbPlanning.PlanningProductionID);
             }
             else
             {
                 return(planningItem.PlanningProductionID);
             }
         }
     }
     catch (Exception ex)
     {
         notification.Type    = Library.DTO.NotificationType.Error;
         notification.Message = Library.Helper.GetInnerException(ex).Message;
         return(-1);;
     }
 }
Пример #2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            DTO.PlanningProductionDTO dtoPlanning = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.PlanningProductionDTO>();
            try
            {
                int?companyID = fw_factory.GetCompanyID(userId);
                using (PlanningProductionEntities context = CreateContext())
                {
                    //delete item is deleted
                    context.PlanningProductionMng_function_DeleteIsDeletedItem();
                    //get bom
                    PlanningProduction dbItem = null;
                    if (id > 0)
                    {
                        dbItem = context.PlanningProduction.Where(o => o.PlanningProductionID == id).FirstOrDefault();
                    }
                    else
                    {
                        dbItem = new PlanningProduction();
                        context.PlanningProduction.Add(dbItem);
                    }
                    if (dbItem == null)
                    {
                        notification.Message = "data not found!";
                        return(false);
                    }
                    else
                    {
                        //create production item for workcenter which is virtual
                        List <ProductionItem> dbVirualProductionItem = new List <ProductionItem>();
                        List <ProductionItem> dbProductionItemResult = new List <ProductionItem>();
                        CreateVirtualProductionItem(dtoPlanning, ref dbVirualProductionItem, ref dbProductionItemResult, companyID, userId);
                        context.ProductionItem.AddRange(dbVirualProductionItem);
                        context.SaveChanges();

                        //convert dto to db
                        converter.DTO2DB_PlanningProduction(dtoPlanning, ref dbItem, dbProductionItemResult);

                        //remove orphan
                        context.PlanningProductionTeam.Local.Where(o => o.PlanningProduction == null).ToList().ForEach(o => context.PlanningProductionTeam.Remove(o));

                        //update workorder
                        int?workOrderID = dtoPlanning.WorkOrderID;
                        var dbWorkOrder = context.WorkOrder.Where(o => o.WorkOrderID == workOrderID).FirstOrDefault();
                        dbWorkOrder.StartDate  = dtoPlanning.WorkOrderStartDate.ConvertStringToDateTime();
                        dbWorkOrder.FinishDate = dtoPlanning.WorkOrderFinishDate.ConvertStringToDateTime();

                        //save data
                        context.SaveChanges();

                        //get return data
                        dtoItem = GetData(dbItem.PlanningProductionID, out notification).Data;

                        //delete item is deleted
                        context.PlanningProductionMng_function_DeleteIsDeletedItem();

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;
                return(false);
            }
        }