示例#1
0
        public Sal_GradeModel Post([Bind] Sal_GradeModel model)
        {
            #region Validate
            ActionService service       = new ActionService(UserLogin);
            string        message       = string.Empty;
            var           checkValidate = false;
            if (model.IsProfileNotGrade)
            {
                checkValidate = HRM.Business.Main.Domain.ValidatorService.OnValidateData <Sal_GradeModel>(model, "Sal_Grade", ref message);
            }
            else if (model.IsCreateByProfile)
            {
                checkValidate = HRM.Business.Main.Domain.ValidatorService.OnValidateData <Sal_GradeModel>(model, "Sal_GradeByProfile", ref message);
                if (!checkValidate)
                {
                    model.ActionStatus = message;
                    return(model);
                }
                return(service.UpdateOrCreate <Sal_GradeEntity, Sal_GradeModel>(model));
            }
            else
            {
                if (model.OrgStructureID != null && model.OrgStructureID != string.Empty)
                {
                    checkValidate = HRM.Business.Main.Domain.ValidatorService.OnValidateData <Sal_GradeModel>(model, "Sal_GradeOrg", ref message);
                }
                else
                {
                    checkValidate = HRM.Business.Main.Domain.ValidatorService.OnValidateData <Sal_GradeModel>(model, "Sal_Grade", ref message);
                }
            }
            if (!checkValidate)
            {
                model.ActionStatus = message;
                return(model);
            }
            #endregion


            string status = string.Empty;
            //string message = string.Empty;
            var hrService     = new Hre_ProfileServices();
            var gradeServices = new Sal_GradeServices();
            if (!string.IsNullOrEmpty(model.OrgStructureID))
            {
                List <Guid> listGuid = new List <Guid>();
                if (model.ProfileIDsExclude != null)
                {
                    var listStr = model.ProfileIDsExclude.Split(',');

                    if (listStr[0] != "")
                    {
                        foreach (var item in listStr)
                        {
                            listGuid.Add(Guid.Parse(item));
                        }
                    }
                }
                List <object> listObj = new List <object>();
                listObj.Add(model.OrgStructureID);
                listObj.Add(string.Empty);
                listObj.Add(string.Empty);
                var lstProfile = hrService.GetData <Hre_ProfileIdEntity>(listObj, ConstantSql.hrm_hr_sp_get_ProfileIdsByOrgStructure, userLogin, ref status).Select(s => s.ID).ToList();
                if (listGuid != null)
                {
                    lstProfile = lstProfile.Where(s => !listGuid.Contains(s)).ToList();
                }
                if (lstProfile.Count == 0 && model.ProfileID == Guid.Empty)
                {
                    model.ActionStatus = ConstantDisplay.HRM_Common_NotEmployee.TranslateString();
                    return(model);
                }

                List <Sal_GradeEntity> lstGradeEntity = new List <Sal_GradeEntity>();

                foreach (var item in lstProfile)
                {
                    Sal_GradeEntity gradeEntity = new Sal_GradeEntity
                    {
                        ProfileID      = item,
                        DateCreate     = model.DateCreate,
                        DateLock       = model.DateLock,
                        GradeCfgName   = model.GradeCfgName,
                        GradePayrollID = model.GradePayrollID,
                        ID             = model.ID,
                        MonthEnd       = model.MonthEnd,
                        MonthStart     = model.MonthStart,
                        IsDelete       = model.IsDelete,
                        UserUpdate     = model.UserCreate,
                        UserCreate     = model.UserCreate,
                    };
                    //model.ActionStatus =  gradeServices.Add(gradeEntity);
                    lstGradeEntity.Add(gradeEntity);
                }
                model.ActionStatus = gradeServices.Add(lstGradeEntity);
                return(model);
            }
            if (model.ProfileIDs != null && model.ProfileIDs != string.Empty)
            {
                var         listStr  = model.ProfileIDs.Split(',');
                List <Guid> listGuid = new List <Guid>();
                if (listStr[0] != "")
                {
                    foreach (var item in listStr)
                    {
                        listGuid.Add(Guid.Parse(item));
                    }
                }
                else
                {
                    model.ActionStatus = ConstantDisplay.HRM_Common_NotEmployee.TranslateString();
                    return(model);
                }
                List <Sal_GradeEntity> lstGradeEntity = new List <Sal_GradeEntity>();
                foreach (var item in listGuid)
                {
                    Sal_GradeEntity gradeEntity = new Sal_GradeEntity
                    {
                        ProfileID      = item,
                        DateCreate     = model.DateCreate,
                        DateLock       = model.DateLock,
                        GradeCfgName   = model.GradeCfgName,
                        GradePayrollID = model.GradePayrollID,
                        ID             = model.ID,
                        MonthEnd       = model.MonthEnd,
                        MonthStart     = model.MonthStart,
                        IsDelete       = model.IsDelete,
                        UserUpdate     = model.UserCreate,
                        UserCreate     = model.UserCreate,
                    };
                    //model.ActionStatus =  gradeServices.Add(gradeEntity);
                    lstGradeEntity.Add(gradeEntity);
                }
                model.ActionStatus = gradeServices.Add(lstGradeEntity);
                return(model);
            }
            if (model.ProfileID == Guid.Empty)
            {
                model.ActionStatus = ConstantDisplay.HRM_Common_NotEmployee.TranslateString();
                return(model);
            }
            return(service.UpdateOrCreate <Sal_GradeEntity, Sal_GradeModel>(model));
        }
        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();
            }
        }