Esempio n. 1
0
 private bool CheckObjectExists(int objectId, int objectType, int parentId)
 {
     try
     {
         T_CommodityAnalysis noName = null;
         noName = db.T_CommodityAnalysis.FirstOrDefault(x => !x.IsDeleted && x.ObjectId == objectId && x.ObjectType == objectType && x.ParentId == parentId);
         if (noName == null)
         {
             return(false);
         }
         return(true);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Esempio n. 2
0
        //Ktra lai
        public ResponseBase Copy_CommoAnaPhaseGroup(int CopyObjectId, int ObjectId, int actionUserId)
        {
            try
            {
                using (db = new IEDEntities())
                {
                    var result   = new ResponseBase();
                    var commoAna = db.T_CommodityAnalysis.FirstOrDefault(x => !x.IsDeleted && x.Id == ObjectId);
                    if (commoAna != null)
                    {
                        //ktra object copy có còn tồn tại hay không
                        var objCopy = db.T_CommodityAnalysis.FirstOrDefault(x => !x.IsDeleted && x.Id == CopyObjectId);
                        if (objCopy != null)
                        {
                            // ktra coi mat hag dang phan tich co tồn tại nhóm công đoạn này chưa
                            var node      = (commoAna.Node + commoAna.Id + ",").Trim();
                            var objExists = db.T_CommodityAnalysis.FirstOrDefault(x => !x.IsDeleted && x.Node.Trim().Equals(node) && x.ObjectId == objCopy.ObjectId);
                            if (objExists != null)
                            {
                                result.IsSuccess = false;
                                result.Errors.Add(new Error()
                                {
                                    MemberName = "", Message = "Mặt Hàng Bạn đang Phân Tích đã Tồn Tại Nhóm Công Đoạn này rồi.\nVui lòng kiểm tra lại.!"
                                });
                            }
                            else
                            {
                                using (TransactionScope scope = new TransactionScope())
                                {
                                    //step 1  -  copy phase group
                                    var new_commoAna = new T_CommodityAnalysis();
                                    new_commoAna.Name        = objCopy.Name;
                                    new_commoAna.ObjectType  = objCopy.ObjectType;
                                    new_commoAna.ObjectId    = objCopy.ObjectId;
                                    new_commoAna.ParentId    = commoAna.Id;
                                    new_commoAna.Node        = node;
                                    new_commoAna.Description = objCopy.Description;
                                    new_commoAna.CreatedUser = actionUserId;
                                    new_commoAna.CreatedDate = DateTime.Now;
                                    db.T_CommodityAnalysis.Add(new_commoAna);
                                    db.SaveChanges();
                                    var parentid = new_commoAna.Id;
                                    //step 2  - copy phase
                                    var commo_ana_phases = db.T_CA_Phase.Where(x => !x.IsDeleted && x.ParentId == objCopy.Id).OrderBy(x => x.CreatedDate);
                                    if (commo_ana_phases != null && commo_ana_phases.Count() > 0)
                                    {
                                        foreach (var item in commo_ana_phases)
                                        {
                                            var new_commoAnaPhase = new T_CA_Phase();
                                            new_commoAnaPhase.Index                    = item.Index;
                                            new_commoAnaPhase.Name                     = item.Name;
                                            new_commoAnaPhase.Code                     = item.Code;
                                            new_commoAnaPhase.PhaseGroupId             = item.PhaseGroupId;
                                            new_commoAnaPhase.Description              = item.Description;
                                            new_commoAnaPhase.EquipmentId              = item.EquipmentId;
                                            new_commoAnaPhase.PhaseGroupId             = item.PhaseGroupId;
                                            new_commoAnaPhase.WorkerLevelId            = item.WorkerLevelId;
                                            new_commoAnaPhase.ParentId                 = item.ParentId;
                                            new_commoAnaPhase.TotalTMU                 = item.TotalTMU;
                                            new_commoAnaPhase.ApplyPressuresId         = item.ApplyPressuresId;
                                            new_commoAnaPhase.PercentWasteEquipment    = item.PercentWasteEquipment;
                                            new_commoAnaPhase.PercentWasteManipulation = item.PercentWasteManipulation;
                                            new_commoAnaPhase.PercentWasteMaterial     = item.PercentWasteMaterial;
                                            new_commoAnaPhase.PercentWasteSpecial      = item.PercentWasteSpecial;
                                            new_commoAnaPhase.Node                     = item.Node;
                                            new_commoAnaPhase.Video                    = item.Video;
                                            new_commoAnaPhase.CreatedUser              = actionUserId;
                                            new_commoAnaPhase.CreatedDate              = new_commoAna.CreatedDate;
                                            // step 3 - copy active timeprepare
                                            var listTimePrepareExist = db.T_CA_Phase_TimePrepare.Where(c => c.Commo_Ana_PhaseId == item.Id && !c.IsDeleted).ToList();
                                            if (listTimePrepareExist.Count > 0)
                                            {
                                                var listTimePrepareNew = new Collection <T_CA_Phase_TimePrepare>();
                                                foreach (var timePrepare in listTimePrepareExist)
                                                {
                                                    listTimePrepareNew.Add(new T_CA_Phase_TimePrepare()
                                                    {
                                                        Commo_Ana_PhaseId = item.Id,
                                                        TimePrepareId     = timePrepare.TimePrepareId,
                                                        CreatedUser       = actionUserId,
                                                        CreatedDate       = new_commoAnaPhase.CreatedDate
                                                    });
                                                }
                                                new_commoAnaPhase.T_CA_Phase_TimePrepare = listTimePrepareNew;
                                            }
                                            //check         // step 4  - copy active manipulation version
                                            var phaseAcc = db.T_CA_Phase_Mani.Where(x => !x.IsDeleted && x.CA_PhaseId == item.Id).ToList();
                                            if (phaseAcc != null && phaseAcc.Count() > 0)
                                            {
                                                new_commoAnaPhase.T_CA_Phase_Mani = new Collection <T_CA_Phase_Mani>();
                                                foreach (var acc in phaseAcc)
                                                {
                                                    var maniC = new T_CA_Phase_Mani();
                                                    maniC.OrderIndex       = acc.OrderIndex;
                                                    maniC.ManipulationId   = acc.ManipulationId;
                                                    maniC.ManipulationCode = acc.ManipulationCode;
                                                    maniC.ManipulationName = acc.ManipulationName;
                                                    maniC.TMUEquipment     = acc.TMUEquipment;
                                                    maniC.TMUManipulation  = acc.TMUManipulation;
                                                    maniC.Loop             = acc.Loop;
                                                    maniC.TotalTMU         = acc.TotalTMU;
                                                    maniC.CreatedUser      = actionUserId;
                                                    maniC.CreatedDate      = new_commoAnaPhase.CreatedDate;
                                                    maniC.T_CA_Phase       = new_commoAnaPhase;
                                                    new_commoAnaPhase.T_CA_Phase_Mani.Add(maniC);
                                                }
                                            }

                                            new_commoAnaPhase.T_CommodityAnalysis = new_commoAna;
                                            // new_commoAnaPhase.ParentId = parentid;
                                            new_commoAnaPhase.Node        = new_commoAna.Node + new_commoAnaPhase.T_CommodityAnalysis.Id + ",";
                                            new_commoAnaPhase.CreatedUser = actionUserId;
                                            new_commoAnaPhase.CreatedDate = new_commoAna.CreatedDate;
                                            db.T_CA_Phase.Add(new_commoAnaPhase);
                                            db.SaveChanges();
                                        }
                                    }
                                    scope.Complete();
                                    result.IsSuccess = true;
                                }
                            }
                        }
                    }
                    else
                    {
                        result.IsSuccess = false;
                        result.Errors.Add(new Error()
                        {
                            MemberName = "", Message = "Dữ liệu bạn đang thao tác đã bị xóa hoặc không tồn tại.\nVui lòng kiểm tra lại.!"
                        });
                    }
                    return(result);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 3
0
        public ResponseBase InsertOrUpdate(T_CommodityAnalysis noNameModel)
        {
            try
            {
                using (db = new IEDEntities())
                {
                    var result = new ResponseBase();
                    T_CommodityAnalysis noName = null;
                    if (CheckObjectExists(noNameModel.ObjectId, noNameModel.ObjectType, noNameModel.ParentId))
                    {
                        result.IsSuccess = false;
                        switch (noNameModel.ObjectType)
                        {
                        case (int)eObjectType.isCommodity:
                            result.Errors.Add(new Error()
                            {
                                MemberName = "Insert  ", Message = "Mặt Hàng này đã được chọn. Vui lòng chọn lại Mặt Hàng khác !."
                            });
                            break;

                        case (int)eObjectType.isWorkShop:
                            result.Errors.Add(new Error()
                            {
                                MemberName = "Insert  ", Message = "Phân xưởng này đã được chọn. Vui lòng chọn lại Phân xưởng khác !."
                            });
                            break;

                        case (int)eObjectType.isPhaseGroup:
                            result.Errors.Add(new Error()
                            {
                                MemberName = "Insert  ", Message = "Nhóm Công Đoạn này đã được chọn. Vui lòng chọn lại Nhóm Công Đoạn khác !."
                            });
                            break;
                        }
                    }
                    else
                    {
                        if (noNameModel.Id == 0)
                        {
                            using (TransactionScope scope = new TransactionScope())
                            {
                                noName = new T_CommodityAnalysis();
                                Parse.CopyObject(noNameModel, ref noName);
                                noName.Node = noName.Node == "0" ? noName.Id.ToString() : noName.Node + noName.ParentId + ",";

                                if (noName.ObjectType == (int)eObjectType.isVersion)
                                {
                                    noName.ObjectId = FindLastedVersion(noName.ParentId, noName.Node);
                                }
                                db.T_CommodityAnalysis.Add(noName);
                                db.SaveChanges();
                                if (noName.ObjectType == (int)eObjectType.isWorkShop)
                                {
                                    T_CommodityAnalysis newObject;
                                    for (int i = 0; i < 3; i++)
                                    {
                                        newObject          = new T_CommodityAnalysis();
                                        newObject.Id       = 0;
                                        newObject.Node     = noName.Node + noName.Id + ",";
                                        newObject.ParentId = noName.Id;
                                        newObject.ObjectId = 0;
                                        switch (i)
                                        {
                                        case 0:
                                            newObject.Name       = "Quy trình công nghệ";
                                            newObject.ObjectType = (int)eObjectType.isGroupVersion;
                                            break;

                                        case 1:
                                            newObject.Name       = "Thiết kế chuyền";
                                            newObject.ObjectType = (int)eObjectType.isLabourDivision;
                                            break;

                                        case 2:
                                            newObject.Name       = "Thành Phần";
                                            newObject.ObjectType = (int)eObjectType.isComponent;
                                            break;
                                        }
                                        newObject.Description = "";
                                        newObject.CreatedUser = noName.CreatedUser;
                                        newObject.CreatedDate = noName.CreatedDate;
                                        db.T_CommodityAnalysis.Add(newObject);
                                    }
                                }
                                db.SaveChanges();
                                scope.Complete();
                                result.IsSuccess = true;
                            }
                        }
                        else
                        {
                            //Update
                            noName = db.T_CommodityAnalysis.FirstOrDefault(x => !x.IsDeleted && x.Id == noNameModel.Id);
                            if (noName == null)
                            {
                                result.IsSuccess = false;
                                result.Errors.Add(new Error()
                                {
                                    MemberName = "Update  ", Message = "Dữ liệu bạn đang thao tác đã bị xóa hoặc không tồn tại. Vui lòng kiểm tra lại !."
                                });
                                return(result);
                            }
                            else
                            {
                                if (noName.ObjectType == (int)eObjectType.isPhaseGroup && noName.Description != noNameModel.Description)
                                {
                                    var phases = db.T_CA_Phase.Where(x => !x.IsDeleted && x.ParentId == noName.Id);
                                    if (phases != null && phases.Count() > 0)
                                    {
                                        foreach (var item in phases)
                                        {
                                            item.Code = noNameModel.Description == null || noNameModel.Description == "" ? item.Index.ToString() : noNameModel.Description + "-" + item.Index;
                                        }
                                    }
                                }
                                noName.Name        = noNameModel.Name;
                                noName.Description = noNameModel.Description;
                                noName.UpdatedUser = noNameModel.UpdatedUser;
                                noName.UpdatedDate = noNameModel.UpdatedDate;
                                db.SaveChanges();
                                result.IsSuccess = true;
                            }
                        }
                    }
                    return(result);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }