Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Compute Payroll New
        /// </summary>
        /// <param name="ProfileID"></param>
        /// <param name="CutOffDuration"></param>
        /// <param name="Sys_AsynTaskID"></param>
        public void ComputePayroll_Progress(ComputePayrollDataModel TotalData, List<Hre_ProfileEntity> ProfileID, Att_CutOffDurationEntity CutOffDuration, Guid Sys_AsynTaskID, int totalProfile, TraceLogManager FileLog, bool ComputeOrderNumber, bool Settlement = false)
        {
            using (var context = new VnrHrmDataContext())
            {
                var unitOfWork = new UnitOfWork(context);
                DateTime DatetimeStart = new DateTime();

                #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_PayrollTableItem> listPayrollTableItem = new List<Sal_PayrollTableItem>();
                List<Sal_PayrollTable> listPayrollTable = new List<Sal_PayrollTable>();

                //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 =>
                //{

                //});

                for (int i = 0; i < ProfileID.Count; i++)
                {
                    TraceLogItemInfo logItem = new TraceLogItemInfo();
                    DatetimeStart = DateTime.Now;
                    var profileItem = ProfileID[i];
                    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 = FindGradePayrollByProfileAndMonthYear(TotalData.listGrade, profileItem.ID, CutOffDuration.DateStart, CutOffDuration.DateEnd);
                    List<Cat_ElementEntity> listElement = new List<Cat_ElementEntity>();
                    if (Grade.GradePayrollID != null && Grade.GradePayrollID != Guid.Empty)
                    {
                        listElement = new List<Cat_ElementEntity>(TotalData.listElement_All.Where(m => (m.GradePayrollID != null && (Guid)m.GradePayrollID == Grade.GradePayrollID) || m.IsApplyGradeAll == true));
                    }

                    //loại bỏ nhân viên có ngày vào làm sau ngày chốt lương
                    Cat_GradePayrollEntity CatGrade = TotalData.listCat_GradePayroll.Where(m => m.ID == Grade.GradePayrollID).FirstOrDefault();
                    if (CatGrade != null && CatGrade.SalaryTimeTypeClose != null)
                    {
                        DateTime DateClose = new DateTime();
                        if (CatGrade.SalaryTimeTypeClose == SalaryTimeTypeClose.E_CURRENTMONTH.ToString())
                        {
                            DateClose = new DateTime(CutOffDuration.MonthYear.Year, CutOffDuration.MonthYear.Month, CatGrade.SalaryDayClose != null ? (int)CatGrade.SalaryDayClose > 30 && CutOffDuration.MonthYear.Month % 2 == 0 ? 30 : (int)CatGrade.SalaryDayClose : 1);
                        }
                        else if (CatGrade.SalaryTimeTypeClose == SalaryTimeTypeClose.E_LASTMONTH.ToString())
                        {
                            DateClose = new DateTime(CutOffDuration.MonthYear.Year, CutOffDuration.MonthYear.Month, CatGrade.SalaryDayClose != null ? (int)CatGrade.SalaryDayClose > 30 && CutOffDuration.MonthYear.Month % 2 == 0 ? 30 : (int)CatGrade.SalaryDayClose : 1).AddMonths(-1);
                        }

                        if (profileItem.DateHire != null && (DateTime)profileItem.DateHire > DateClose)
                        {
                            isCancled = true;
                            FileLog.WriteLog("NV có ngày vào làm sau ngày chốt lương của tháng", "", "");
                        }
                    }

                    if (!isCancled)
                    {
                        #region tạo mới PayrollTable
                        Sal_PayrollTable PayrollTable_Model = new Sal_PayrollTable();
                        if (Settlement == true)
                        {
                            if (repoSal_PayrollTable.FindBy(m => m.CutOffDurationID == CutOffDuration.ID && m.ProfileID == profileItem.ID).FirstOrDefault() != null)
                            {
                                Att_CutOffDurationEntity CutOff = TotalData.listCutOffDuration.Where(m => m.MonthYear >= CutOffDuration.MonthYear).OrderBy(m => m.MonthYear).FirstOrDefault();
                                PayrollTable_Model = new Sal_PayrollTable()
                                {
                                    ID = Guid.NewGuid(),
                                    ProfileID = profileItem.ID,
                                    CutOffDurationID = CutOff.ID,
                                    MonthYear = CutOff.MonthYear,
                                    OrgStructureID = profileItem.OrgStructureID,
                                    PositionID = profileItem.PositionID,
                                    JobTitleID = profileItem.JobTitleID,
                                    EmployeeTypeID = profileItem.EmpTypeID,
                                    PayrollGroupID = profileItem.PayrollGroupID,
                                    CostCentreID = profileItem.CostCentreID,
                                    IncomeBeforeTax = 0,
                                    IsCash = profileItem.IsCash,
                                    DependantCount = 0,
                                    IncomeTaxable = 0,
                                    AmountPaidPITCom = 0,
                                    AmountPaidPITEmp = 0,
                                    IncomeNET = 0,
                                    BankID = profileItem.BankID,
                                    AccountNo = profileItem.AccountNo,
                                    Status = FileLog.GetFullPath(FileLog.FileName).Replace(".XML", "_" + DateTime.Now.ToString("yyyyMMdd") + ".XML"),
                                };
                            }
                            else
                            {
                                PayrollTable_Model = new Sal_PayrollTable()
                                {
                                    ID = Guid.NewGuid(),
                                    ProfileID = profileItem.ID,
                                    CutOffDurationID = CutOffDuration.ID,
                                    MonthYear = CutOffDuration.MonthYear,
                                    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,
                                    Status = FileLog.GetFullPath(FileLog.FileName).Replace(".XML", "_" + DateTime.Now.ToString("yyyyMMdd") + ".XML"),
                                };
                            }
                        }
                        else
                        {
                            PayrollTable_Model = new Sal_PayrollTable()
                            {
                                ID = Guid.NewGuid(),
                                ProfileID = profileItem.ID,
                                CutOffDurationID = CutOffDuration.ID,
                                MonthYear = CutOffDuration.MonthYear,
                                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,
                                //Status = FileLog.GetFullPath(FileLog.FileName).Replace(".XML", "_" + DateTime.Now.ToString("yyyyMMdd") + ".XML"),
                            };
                        }

                        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
                        {
                            //DatetimeStart = DateTime.Now;
                            listElementFormula = ParseElementFormula(listElementFormula, listElement, TotalData, profileItem, CutOffDuration, listTmpDeduction, ComputeOrderNumber, FileLog);
                            //FileLog.WriteLog("", "Compute Formula", Common.ComputeTime(DatetimeStart, DateTime.Now));

                            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_PayrollTableItem tableItem = new Sal_PayrollTableItem();
                                tableItem.ID = Guid.NewGuid();
                                tableItem.PayrollTableID = PayrollTable_Model.ID;
                                tableItem.Name = elementItem.ElementName;
                                tableItem.Code = elementItem.ElementCode;
                                tableItem.MonthYear = CutOffDuration.MonthYear;
                                tableItem.IsDecrypt = false;
                                tableItem.IsAddToHourlyRate = false;
                                tableItem.IsChargePIT = false;
                                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.Description4 += "E_BOLD,";
                                }
                                tableItem.Description4 += elementItem.ElementLevel + "," + elementItem.Type;
                                var ElementResult = listElementFormula.Where(m => m.VariableName.Trim() == elementItem.ElementCode.Trim());
                                if (ElementResult != null)
                                {
                                    tableItem.Value = ElementResult.LastOrDefault().Value.ToString();
                                    tableItem.Description1 = ElementResult.LastOrDefault().ErrorMessage;
                                }
                                else
                                {
                                    tableItem.Value = "0";
                                    tableItem.Description1 = "Không Tìm Thấy Phần Tử !";
                                }

                                lock (listPayrollTableItem)
                                {
                                    listPayrollTableItem.Add(tableItem);
                                }
                            }
                            logItem.Summary += profileItem.CodeEmp + "-" + profileItem.ProfileName + "-" + Common.ComputeTime(DatetimeStart, DateTime.Now) + "-" + listElement.Count.ToString() + "Element Formula";
                            logItem.Source = "Sucess";
                            FileLog.WriteLog(logItem);
                        }
                        catch (Exception ex)
                        {
                            logItem.Summary += profileItem.CodeEmp + "-" + profileItem.ProfileName + "-" + Common.ComputeTime(DatetimeStart, DateTime.Now) + "-" + listElement.Count.ToString() + " Element Formula";
                            logItem.Description = ex.Message;
                            logItem.Source = "Error";
                            FileLog.WriteLog(logItem);
                            //FileLog.WriteLog("Error", "Error Compute Profle " + ProfileID[i].CodeEmp + "-" + ProfileID[i].ProfileName, Common.ComputeTime(DatetimeStart, DateTime.Now));

                            Sal_PayrollTableItem tableItem = new Sal_PayrollTableItem();
                            tableItem.ID = Guid.NewGuid();
                            tableItem.PayrollTableID = PayrollTable_Model.ID;
                            tableItem.Name = "Lỗi, không thể tính được phần tử " + ex.Message;
                            tableItem.Code = "Error";
                            tableItem.MonthYear = CutOffDuration.MonthYear;
                            tableItem.IsDecrypt = false;
                            tableItem.IsAddToHourlyRate = false;
                            tableItem.IsChargePIT = false;
                            tableItem.OrderNo = 0;
                            tableItem.ElementType = "Payroll";
                            tableItem.ValueType = "Double";
                            tableItem.Description4 = "Double";
                            tableItem.Value = "0";
                            tableItem.Description1 = 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).Replace(".XML", "_" + DateTime.Now.ToString("yyyyMMdd") + ".XML");
                asynTask.TimeEnd = DateTime.Now;

                if (asynTask.PercentComplete >= 1)
                {
                    asynTask.Status = AsynTaskStatus.Done.ToString();
                    asynTask.PercentComplete = 1D;
                }

                //if (dataErrorCode == DataErrorCode.Locked)
                //{
                //    asynTask.PercentComplete = 1D;
                //    asynTask.Description = "Dữ Liệu Tính Lương Đã Bị Khóa";
                //    FileLog.WriteLog("Dữ liệu tính lương bị khóa", "--------------------", "--------------------");
                //}

                DatetimeStart = DateTime.Now;

                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_PayrollTable).Name });
                        var dtPayrollTableItem = commander.GetSchema("Columns", new string[] { null, null, typeof(Sal_PayrollTableItem).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_PayrollTable).Name, payrollTableFields);

                        foreach (var listPayrollTableItemBySize in listPayrollTableItem.Chunk(2000))
                        {
                            try
                            {
                                bulkCopyHelper.WriteToServer(listPayrollTableItemBySize.ToList(),
                                       typeof(Sal_PayrollTableItem).Name, payrollTableItemFields);
                            }
                            catch (Exception ex)
                            {
                                FileLog.WriteLog(ex.Message, "", "");
                            }
                        }
                    }
                    else
                    {
                        var dtPayrollTable = commander.GetSchema("Columns", new string[] { null, typeof(Sal_PayrollTable).Name });
                        var dtPayrollTableItem = commander.GetSchema("Columns", new string[] { null, typeof(Sal_PayrollTableItem).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_PayrollTable).Name, listPayrollTable, payrollTableFields);

                        foreach (var listPayrollTableItemBySize in listPayrollTableItem.Chunk(2000))
                        {
                            commander.InsertList(typeof(Sal_PayrollTableItem).Name, listPayrollTableItemBySize.ToList(), payrollTableItemFields);
                        }
                    }
                }
                FileLog.WriteLog("", "", "Save Change 100 Profile-" + Common.ComputeTime(DatetimeStart, DateTime.Now));

                //repoPayrollTableItem.Add(listPayrollTableItem);
                //repoPayrollTable.Add(listPayrollTable);
                unitOfWork.SaveChanges();
            }
        }
Ejemplo n.º 3
0
        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();
            }
        }