Exemplo n.º 1
0
        public void ComputeBonusUnusualAllowance_Progress(Guid AsynTaskID, Sal_UnusualAllowanceEntity model, bool AllowanceEvaluationYear = false)
        {
            using (var context = new VnrHrmDataContext())
            {
                var unitOfWork                  = (IUnitOfWork) new UnitOfWork(context);
                var repoSys_AsynTask            = new CustomBaseRepository <Sys_AsynTask>(unitOfWork);
                var repoCat_Element             = new CustomBaseRepository <Cat_Element>(unitOfWork);
                var repoCat_UnusualAllowanceCfg = new CustomBaseRepository <Cat_UnusualAllowanceCfg>(unitOfWork);
                var repoUnusualAllowance        = new CustomBaseRepository <Sal_UnusualAllowance>(unitOfWork);

                Sys_AsynTask asynTask = repoSys_AsynTask.GetById(AsynTaskID);

                Sal_ComputePayrollServices CumputePayrollServices = new Sal_ComputePayrollServices();
                List <object> listModel = new List <object>();
                string        status    = string.Empty;
                Sal_ComputePayrollServices        Services                 = new Sal_ComputePayrollServices();
                List <Sal_UnusualAllowanceEntity> ListResult               = new List <Sal_UnusualAllowanceEntity>();
                List <ElementFormula>             listElementFormula       = new List <ElementFormula>();
                Sal_UnusualAllowanceServices      UnusualAllowanceServices = new Sal_UnusualAllowanceServices();

                Att_CutOffDurationEntity CutOffDuration = new Att_CutOffDurationEntity();
                if (model.MonthEnd != null)
                {
                    CutOffDuration.MonthYear = model.MonthStart.Value;
                    CutOffDuration.DateStart = model.MonthStart.Value;
                    CutOffDuration.DateEnd   = model.MonthEnd.Value;
                }
                else
                {
                    CutOffDuration.MonthYear = new DateTime(model.MonthStart.Value.Year, model.MonthStart.Value.Month, 1);
                    CutOffDuration.DateStart = new DateTime(model.MonthStart.Value.Year, model.MonthStart.Value.Month, 1);
                    CutOffDuration.DateEnd   = new DateTime(model.MonthStart.Value.Year, model.MonthStart.Value.Month, 1).AddMonths(1).AddDays(-1);
                }

                List <Hre_ProfileEntity> ListProfile = new List <Hre_ProfileEntity>();

                if (model.OrgStructureIDs != null && model.OrgStructureIDs != string.Empty)
                {
                    listModel = new List <object>();
                    listModel.AddRange(new object[18]);
                    listModel[2]  = model.OrgStructureIDs;
                    listModel[16] = 1;
                    listModel[17] = Int32.MaxValue - 1;
                    ListProfile   = GetData <Hre_ProfileEntity>(listModel, ConstantSql.hrm_hr_sp_get_Profile, ref status);
                    if (model.ExProfileIDs != null)
                    {
                        string[] ExProfile = model.ExProfileIDs.Split(',');
                        ListProfile = ListProfile.Where(m => ExProfile.Contains(m.ID.ToString())).ToList();
                    }
                }
                else if (model.ProfileIDs != null && model.ProfileIDs != string.Empty)
                {
                    ListProfile = GetData <Hre_ProfileEntity>(Common.DotNetToOracle(model.ProfileIDs), ConstantSql.hrm_hr_sp_get_ProfileByIds, ref status);
                }


                //lọc profile theo workplace
                if (model.WorkingPlaceID != null)
                {
                    string[] listWorkPlare = model.WorkingPlaceID.Split(',');
                    ListProfile = ListProfile.Where(m => listWorkPlare.Contains(m.WorkPlaceID.ToString())).ToList();
                }

                List <Cat_UnusualAllowanceCfg> listUnusualAllowanceCfg = new List <Cat_UnusualAllowanceCfg>();
                listUnusualAllowanceCfg = repoCat_UnusualAllowanceCfg.FindBy(m => m.IsDelete != true).ToList();

                #region Delete các phụ cấp trong năm đã có
                List <Sal_UnusualAllowance> listUnusualAllowance = repoUnusualAllowance.FindBy(m => m.IsDelete != true && m.MonthStart != null && model.MonthStart != null && m.MonthStart.Value.Year == model.MonthStart.Value.Year && m.Status != WorkHistoryStatus.E_APPROVED.ToString()).ToList();

                //nếu là tính phép năm sức khỏe tốt thì delete thêm loại GoodHealth
                if (AllowanceEvaluationYear)
                {
                    var _tmp = listUnusualAllowanceCfg.FirstOrDefault(m => m.Code == "GoodHealth");
                    listUnusualAllowance = listUnusualAllowance.Where(m => (m.UnusualEDTypeID == model.UnusualEDTypeID || m.UnusualEDTypeID == _tmp.ID) && ListProfile.Any(t => t.ID == m.ProfileID)).ToList();
                }
                else
                {
                    listUnusualAllowance = listUnusualAllowance.Where(m => m.UnusualEDTypeID == model.UnusualEDTypeID && ListProfile.Any(t => t.ID == m.ProfileID)).ToList();
                }
                //update isdelete is true
                listUnusualAllowance.ForEach(m => m.IsDelete = true);
                #endregion

                //Cat_ElementEntity ElementItem = repoCat_Element.GetById((Guid)model.Element).Copy<Cat_ElementEntity>();
                Cat_ElementEntity ElementItem = repoCat_Element.FindBy(m => m.ID == (Guid)model.Element).FirstOrDefault().Copy <Cat_ElementEntity>();


                ComputePayrollDataModel TotalData = Services.GetDataForComputeSalary(CutOffDuration);

                List <Cat_ElementEntity> listElementAll = new List <Cat_ElementEntity>();
                listElementAll = TotalData.listElement_All;

                #region Xử lý cho tính phép năm, sức khỏe tốt
                List <Cat_UnusualAllowanceCfg> ListUnusualAllowanceCfg = new List <Cat_UnusualAllowanceCfg>();
                Cat_UnusualAllowanceCfg        PaidLeave  = null;
                Cat_UnusualAllowanceCfg        GoodHealth = null;
                ElementFormula FormulaValue = new ElementFormula();
                if (AllowanceEvaluationYear)
                {
                    ListUnusualAllowanceCfg = repoCat_UnusualAllowanceCfg.FindBy(m => m.IsDelete != true).ToList();
                    PaidLeave  = ListUnusualAllowanceCfg.FirstOrDefault(m => m.Code == "PaidLeave");
                    GoodHealth = ListUnusualAllowanceCfg.FirstOrDefault(m => m.Code == "GoodHealth");
                }
                #endregion

                foreach (var profile in ListProfile)
                {
                    if (AllowanceEvaluationYear)
                    {
                        #region Tính Công Thức
                        try
                        {
                            listElementFormula        = new List <ElementFormula>();
                            TotalData.listElement_All = repoCat_Element.FindBy(m => m.IsDelete != true).ToList().Translate <Cat_ElementEntity>();
                            listElementFormula        = CumputePayrollServices.ParseFormula(ElementItem, listElementFormula, TotalData, profile, CutOffDuration, new Dictionary <Guid, ValueCount>());
                        }
                        catch
                        {
                            listElementFormula = null;
                            continue;
                        }
                        #endregion

                        if (PaidLeave != null)
                        {
                            //lưu phần tử TienBuPhepNam
                            Sal_UnusualAllowance UnusualAllowance = new Sal_UnusualAllowance();
                            UnusualAllowance.ID              = Guid.NewGuid();
                            UnusualAllowance.ProfileID       = profile.ID;
                            UnusualAllowance.UnusualEDTypeID = PaidLeave.ID;

                            UnusualAllowance.MonthStart = CutOffDuration.DateStart;
                            UnusualAllowance.MonthEnd   = CutOffDuration.DateEnd;
                            UnusualAllowance.Type       = EnumDropDown.EDType.E_EARNING.ToString();
                            UnusualAllowance.CurrencyID = model.CurrencyID;
                            FormulaValue            = listElementFormula.Where(m => m.VariableName.ReplaceSpace() == "TienPhepNam").FirstOrDefault();
                            UnusualAllowance.Amount = double.Parse(FormulaValue != null ? FormulaValue.Value.ToString() : "0");
                            repoUnusualAllowance.Add(UnusualAllowance);
                        }

                        if (GoodHealth != null)
                        {
                            //lưu phần tử TienSucKhoe
                            Sal_UnusualAllowance UnusualAllowance = new Sal_UnusualAllowance();
                            UnusualAllowance                 = new Sal_UnusualAllowance();
                            UnusualAllowance.ID              = Guid.NewGuid();
                            UnusualAllowance.ProfileID       = profile.ID;
                            UnusualAllowance.UnusualEDTypeID = GoodHealth.ID;

                            UnusualAllowance.MonthStart = CutOffDuration.DateStart;
                            UnusualAllowance.MonthEnd   = CutOffDuration.DateEnd;
                            UnusualAllowance.Type       = EnumDropDown.EDType.E_EARNING.ToString();
                            UnusualAllowance.CurrencyID = model.CurrencyID;
                            FormulaValue            = listElementFormula.Where(m => m.VariableName.ReplaceSpace() == "TienSucKhoe").FirstOrDefault();
                            UnusualAllowance.Amount = double.Parse(FormulaValue != null ? FormulaValue.Value.ToString() : "0");
                            repoUnusualAllowance.Add(UnusualAllowance);
                        }
                    }
                    else
                    {
                        Sal_UnusualAllowance UnusualAllowance = new Sal_UnusualAllowance();
                        UnusualAllowance.ID              = Guid.NewGuid();
                        UnusualAllowance.ProfileID       = profile.ID;
                        UnusualAllowance.UnusualEDTypeID = model.UnusualEDTypeID;

                        UnusualAllowance.MonthStart = CutOffDuration.DateStart;
                        UnusualAllowance.MonthEnd   = CutOffDuration.DateEnd;
                        UnusualAllowance.Type       = EnumDropDown.EDType.E_EARNING.ToString();
                        UnusualAllowance.CurrencyID = model.CurrencyID;
                        try
                        {
                            listElementFormula        = new List <ElementFormula>();
                            TotalData.listElement_All = repoCat_Element.FindBy(m => m.IsDelete != true).ToList().Translate <Cat_ElementEntity>();
                            listElementFormula        = CumputePayrollServices.ParseFormula(ElementItem, listElementFormula, TotalData, profile, CutOffDuration, new Dictionary <Guid, ValueCount>());
                            FormulaValue            = listElementFormula.Where(m => m.VariableName.ReplaceSpace() == ElementItem.ElementCode.ReplaceSpace()).FirstOrDefault();
                            UnusualAllowance.Amount = double.Parse(FormulaValue != null ? FormulaValue.Value.ToString() : "0");
                        }
                        catch
                        {
                            UnusualAllowance.Amount = 0;
                            UnusualAllowance.Notes  = "Không tính được công thức - Lỗi: " + TotalData.Status;
                        }
                        repoUnusualAllowance.Add(UnusualAllowance);
                    }

                    if (ListProfile.IndexOf(profile) % 10 == 0)
                    {
                        asynTask.PercentComplete = (double)(ListProfile.IndexOf(profile) + 1) / (double)ListProfile.Count;
                        unitOfWork.SaveChanges();
                    }
                }
                asynTask.PercentComplete = 1;
                unitOfWork.SaveChanges();
            }
        }