public void ComputeCommission(List <Hre_ProfileEntity> listProfile, Att_CutOffDurationEntity CutOffDuration, Guid asynTaskID, string HeaderTemplateLog, string methodPayroll, Guid CutOffDuration2ID, string UserLogin) { using (var context = new VnrHrmDataContext()) { var unitOfWork = (IUnitOfWork) new UnitOfWork(context); var repoSys_AsynTask = new CustomBaseRepository <Sys_AsynTask>(unitOfWork); var repoPayrollTable = new CustomBaseRepository <Sal_PayrollTable>(unitOfWork); var Sys_Model = repoSys_AsynTask.FindBy(m => m.ID == asynTaskID).FirstOrDefault(); if (!DeletePayrollTable(listProfile.Select(d => d.ID).ToList(), CutOffDuration.ID)) { Sys_Model.Status = AsynTaskStatus.Error.ToString(); Sys_Model.Description = "Không thể xóa bảng lương cũ"; Sys_Model.PercentComplete = 1; unitOfWork.SaveChanges(); } else { #region Get All Data DateTime DatetimeStart = DateTime.Now; ComputePayrollDataModel TotalData = PayrollServices.GetDataForComputeSalary(CutOffDuration, UserLogin); //FileLog.WriteLog("", "Get Data", Common.ComputeTime(DatetimeStart, DateTime.Now)); #endregion //Kiểm tra xem có lỗi store hay không if (TotalData.Status != null && TotalData.Status != string.Empty) { Sys_Model.Status = AsynTaskStatus.Error.ToString(); Sys_Model.Description = TotalData.Status; Sys_Model.PercentComplete = 1; unitOfWork.SaveChanges(); } else { #region Ghi Log //Lấy đường dẫn thư mục ghi log //set lại thư mục ghi log //WriteLog.SettingPath = Common.GetPath("Log"); //WriteLog.DynamicDirectory = "ComputePayroll" + DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss"); //WriteLog.FileName = "LogFile"; //WriteLog.WriteLog(HeaderTemplateLog, "", ""); #endregion int pageSize = 100; //mỗi lần tính cho 100 nhân viên int totalProfile = listProfile.Count; //Tổng số nhâ viên foreach (var listProfileBySize in listProfile.Chunk(pageSize)) { ComputeCommission_Progress(TotalData, listProfileBySize.ToList(), CutOffDuration, Sys_Model.ID, totalProfile, methodPayroll, CutOffDuration2ID); } } } } }
public void ComputeCommission_Progress(ComputePayrollDataModel TotalData, List <Hre_ProfileEntity> ProfileID, Att_CutOffDurationEntity CutOffDuration, Guid Sys_AsynTaskID, int totalProfile, string methodPayroll, Guid CutOffDuration2ID) { using (var context = new VnrHrmDataContext()) { TraceLogManager FileLog = new TraceLogManager(); var unitOfWork = new UnitOfWork(context); #region Init Repo var repoSys_AsynTask = new CustomBaseRepository <Sys_AsynTask>(unitOfWork); var repoHre_profile = new CustomBaseRepository <Hre_ProfileEntity>(unitOfWork); var repoCat_Element = new CustomBaseRepository <Cat_ElementEntity>(unitOfWork); var repoSal_BasicSalary = new CustomBaseRepository <Sal_BasicSalaryEntity>(unitOfWork); //var repoPayrollTable = new CustomBaseRepository<Sal_PayrollTable>(unitOfWork); //var repoPayrollTableItem = new CustomBaseRepository<Sal_PayrollTableItem>(unitOfWork); var repoCat_OvertimeType = new CustomBaseRepository <Cat_OvertimeTypeEntity>(unitOfWork); var repoCat_LeaveDayType = new CustomBaseRepository <Cat_LeaveDayTypeEntity>(unitOfWork); var repoCat_UsualAllowanceEntity = new CustomBaseRepository <Cat_UsualAllowanceEntity>(unitOfWork); var repoCat_GradePayroll = new CustomBaseRepository <Cat_GradePayrollEntity>(unitOfWork); var repoCat_UnusualAllowanceCfg = new CustomBaseRepository <Cat_UnusualAllowanceCfgEntity>(unitOfWork); var repoSal_PayrollTable = new CustomBaseRepository <Sal_PayrollTable>(unitOfWork); #endregion //Các biến xử dụng chung Sys_AsynTask asynTask = new Sys_AsynTask(); //[SCV] list lưu tiền khấu nhân viên chưa đủ thâm niên của shop Dictionary <Guid, ValueCount> listTmpDeduction = new Dictionary <Guid, ValueCount>(); List <Sal_PayCommissionItem> listPayrollTableItem = new List <Sal_PayCommissionItem>(); List <Sal_PayCommission> listPayrollTable = new List <Sal_PayCommission>(); //Get asynTask asynTask = repoSys_AsynTask.FindBy(m => m.ID == Sys_AsynTaskID).FirstOrDefault(); //Order By theo ngày vào làm để tính trường hợp nhân viên không đủ thâm niên của dự án SCV ProfileID = ProfileID.OrderBy(m => m.DateHire).ToList(); ParallelOptions parallelOptions = new ParallelOptions(); #region Duyệt Profile Parallel.For(0, ProfileID.Count, parallelOptions, d => { var profileItem = ProfileID[d]; bool isCancled = false; //Biến tổng lưu tất cả các value của Formula List <ElementFormula> listElementFormula = new List <ElementFormula>(); //Lấy các phần tử tính lương nằm trong Grade của nhân viên Sal_GradeEntity Grade = PayrollServices.FindGradePayrollByProfileAndMonthYear(TotalData.listGrade, profileItem.ID, CutOffDuration.DateStart, CutOffDuration.DateEnd); //lấy phần tử tính lương theo grade List <Cat_ElementEntity> listElement = TotalData.listElement_All.Where(m => m.GradePayrollID != null && (Guid)m.GradePayrollID == Grade.GradePayrollID && m.MethodPayroll != null && m.MethodPayroll == methodPayroll).ToList(); if (!isCancled) { #region tạo mới PayrollTable Sal_PayCommission PayrollTable_Model = new Sal_PayCommission() { ID = Guid.NewGuid(), ProfileID = profileItem.ID, CutoffDurationID = CutOffDuration.ID, MonthYear = CutOffDuration.MonthYear, CutoffDuration2ID = CutOffDuration2ID, //OrgStructureID = profileItem.OrgStructureID, //PositionID = profileItem.PositionID, //JobTitleID = profileItem.JobTitleID, //EmployeeTypeID = profileItem.EmpTypeID, //PayrollGroupID = profileItem.PayrollGroupID, //CostCentreID = profileItem.CostCentreID, //IncomeBeforeTax = 0, //DependantCount = 0, //IncomeTaxable = 0, //AmountPaidPITCom = 0, //AmountPaidPITEmp = 0, //IncomeNET = 0, //BankID = profileItem.BankID, //AccountNo = profileItem.AccountNo, }; lock (listPayrollTable) { listPayrollTable.Add(PayrollTable_Model); } #endregion //bắt lỗi tính công thức có giá trị nào null hay không try { listElementFormula = PayrollServices.ParseElementFormula(listElementFormula, listElement, TotalData, profileItem, CutOffDuration, listTmpDeduction, false, FileLog); listElement = listElement.OrderBy(m => m.OrderNumber).ToList();//sắp xếp lại //Duyệt qua các phần tử tính lương foreach (var elementItem in listElement) { Sal_PayCommissionItem tableItem = new Sal_PayCommissionItem(); tableItem.ID = Guid.NewGuid(); tableItem.PayCommissionID = PayrollTable_Model.ID; tableItem.Name = elementItem.ElementName; tableItem.Code = elementItem.ElementCode; tableItem.OrderNo = elementItem.OrderNumber != null ? (int)elementItem.OrderNumber : 0; tableItem.ElementType = elementItem.TabType; tableItem.ValueType = elementItem.Type; tableItem.IsShow = elementItem.Invisible != null ? !elementItem.Invisible : true; if (elementItem.IsBold == true) { tableItem.Description += "E_BOLD,"; } tableItem.Description += elementItem.ElementLevel + "," + elementItem.Type; var ElementResult = listElementFormula.Where(m => m.VariableName.Trim() == elementItem.ElementCode.Trim()).FirstOrDefault(); if (ElementResult != null) { tableItem.Value = ElementResult.Value.ToString(); tableItem.Description = ElementResult.ErrorMessage; } else { tableItem.Value = "0"; tableItem.Description = "Không Tìm Thấy Phần Tử !"; } lock (listPayrollTableItem) { listPayrollTableItem.Add(tableItem); } } } catch (Exception ex) { Sal_PayCommissionItem tableItem = new Sal_PayCommissionItem(); tableItem.ID = Guid.NewGuid(); tableItem.PayCommissionID = PayrollTable_Model.ID; tableItem.Name = "Lỗi, không thể tính được phần tử " + ex.Message; tableItem.Code = "Error"; tableItem.OrderNo = 0; tableItem.ElementType = "Payroll"; tableItem.ValueType = "Double"; tableItem.Value = "0"; tableItem.Description = ex.Message; lock (listPayrollTableItem) { listPayrollTableItem.Add(tableItem); } } } }); #endregion asynTask = repoSys_AsynTask.FindBy(m => m.ID == Sys_AsynTaskID).FirstOrDefault(); asynTask.PercentComplete += ((double)ProfileID.Count / (double)totalProfile); asynTask.Description = FileLog.GetFullPath(FileLog.FileName); asynTask.TimeEnd = DateTime.Now; if (asynTask.PercentComplete >= 1) { asynTask.Status = AsynTaskStatus.Done.ToString(); asynTask.PercentComplete = 1D; } var connection = context.Database.Connection.GetAdoConnection(); using (DbCommander commander = new DbCommander(connection)) { if (connection.IsSqlConnection()) { var bulkCopyHelper = new SqlBulkCopyHelper(connection.ConnectionString); var dtPayrollTable = commander.GetSchema("Columns", new string[] { null, null, typeof(Sal_PayCommission).Name }); var dtPayrollTableItem = commander.GetSchema("Columns", new string[] { null, null, typeof(Sal_PayCommissionItem).Name }); var payrollTableFields = dtPayrollTable.Rows.OfType <System.Data.DataRow>().Select(d => d["Column_Name"].GetString()).ToArray(); var payrollTableItemFields = dtPayrollTableItem.Rows.OfType <System.Data.DataRow>().Select(d => d["Column_Name"].GetString()).ToArray(); bulkCopyHelper.WriteToServer(listPayrollTable, typeof(Sal_PayCommission).Name, payrollTableFields); foreach (var listPayrollTableItemBySize in listPayrollTableItem.Chunk(2000)) { bulkCopyHelper.WriteToServer(listPayrollTableItemBySize.ToList(), typeof(Sal_PayCommissionItem).Name, payrollTableItemFields); } } else { var dtPayrollTable = commander.GetSchema("Columns", new string[] { null, typeof(Sal_PayCommission).Name }); var dtPayrollTableItem = commander.GetSchema("Columns", new string[] { null, typeof(Sal_PayCommissionItem).Name }); var payrollTableFields = dtPayrollTable.Rows.OfType <System.Data.DataRow>().Select(d => d["Name"].GetString()).ToArray(); var payrollTableItemFields = dtPayrollTableItem.Rows.OfType <System.Data.DataRow>().Select(d => d["Name"].GetString()).ToArray(); commander.InsertList(typeof(Sal_PayCommission).Name, listPayrollTable, payrollTableFields); foreach (var listPayrollTableItemBySize in listPayrollTableItem.Chunk(2000)) { commander.InsertList(typeof(Sal_PayCommissionItem).Name, listPayrollTableItemBySize.ToList(), payrollTableItemFields); } } } //repoPayrollTableItem.Add(listPayrollTableItem); //repoPayrollTable.Add(listPayrollTable); unitOfWork.SaveChanges(); } }
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(); } }