public async Task <List <PS_PayrollPeriod_Dto> > GetAllBySearchAsync(PS_PayrollPeriod_Search_Dto data) { try { List <PS_PayrollPeriod_Dto> result = await Task.Run(() => Repository .Include(x => x.PayPeriods) .Select(MapToGetListOutputDto).ToList()); for (int i = 0; i < result.Count; i++) { if (!CustomHelpers.CheckIfMatches(result[i], data)) { result.Remove(result[i]); } } return(result); } catch (Exception ex) { return(null); } }
public async Task <IActionResult> OnPostPayrollPeriod() { if (ModelState.IsValid) { try { var FormData = Request.Form; bool isSearching = JsonSerializer.Deserialize <bool>(FormData["isSearching"]); if (isSearching) { PS_PayrollPeriod_Search_Dto payrollPeriodSearch_Dto = JsonSerializer.Deserialize <PS_PayrollPeriod_Search_Dto>(FormData["info"]); List <PS_PayrollPeriod_Dto> result = new List <PS_PayrollPeriod_Dto>(); //result = await PS_PayrollPeriodAppService.GetAllBySearchAsync(payrollPeriodSearch_Dto); return(StatusCode(200, result)); } else { PS_PayrollPeriod_Dto payrollPeriod_Dto = JsonSerializer.Deserialize <PS_PayrollPeriod_Dto>(FormData["info"]); bool IsEditing = payrollPeriod_Dto.Id > 0; if (IsEditing) { PS_PayrollPeriod curPayrollPeriod = await PS_PayrollPeriodAppService.GetPayrollPeriodRawAsync(payrollPeriod_Dto.Id); EntityChangeInfo entityChangeInfo = new EntityChangeInfo(); if (AuditingManager.Current != null) { entityChangeInfo.EntityId = payrollPeriod_Dto.Id.ToString(); entityChangeInfo.EntityTenantId = payrollPeriod_Dto.TenantId; entityChangeInfo.ChangeTime = DateTime.Now; entityChangeInfo.ChangeType = EntityChangeType.Updated; entityChangeInfo.EntityTypeFullName = typeof(PS_PayrollPeriod_Dto).FullName; entityChangeInfo.PropertyChanges = new List <EntityPropertyChangeInfo>(); List <EntityPropertyChangeInfo> entityPropertyChanges = new List <EntityPropertyChangeInfo>(); var auditProps = typeof(PS_PayrollPeriod_Dto).GetProperties().Where(x => Attribute.IsDefined(x, typeof(CustomAuditedAttribute))).ToList(); PS_PayrollPeriod mappedInput = ObjectMapper.Map <PS_PayrollPeriod_Dto, PS_PayrollPeriod>(payrollPeriod_Dto); foreach (var prop in auditProps) { EntityPropertyChangeInfo propertyChange = new EntityPropertyChangeInfo(); object origVal = prop.GetValue(curPayrollPeriod); propertyChange.OriginalValue = origVal == null ? "" : origVal.ToString(); object newVal = prop.GetValue(mappedInput); propertyChange.NewValue = newVal == null ? "" : newVal.ToString(); if (propertyChange.OriginalValue == propertyChange.NewValue) { continue; } propertyChange.PropertyName = prop.Name; //if (prop.Name.EndsWith("Id")) //{ // try // { // string valuePropName = prop.Name.TrimEnd('d', 'I'); // propertyChange.PropertyName = valuePropName; // var valueProp = typeof(PS_PayrollPeriod).GetProperty(valuePropName); // DictionaryValue _origValObj = (DictionaryValue)valueProp.GetValue(payrollPeriod_Dto); // if (_origValObj == null) _origValObj = await DictionaryValuesRepo.GetAsync((Guid)origVal); // string _origVal = _origValObj.Value; // propertyChange.OriginalValue = origVal == null ? "" : _origVal; // DictionaryValue _newValObj = (DictionaryValue)valueProp.GetValue(mappedInput); // if (_newValObj == null) _newValObj = await DictionaryValuesRepo.GetAsync((Guid)newVal); // string _newVal = _newValObj.Value; // propertyChange.NewValue = _newValObj == null ? "" : _newVal; // } // catch (Exception ex) // { // } //} propertyChange.PropertyTypeFullName = prop.Name.GetType().FullName; entityChangeInfo.PropertyChanges.Add(propertyChange); } #region ExtraProperties //List<EmployeeExtraPropertyHistory> allExtraPropertyHistories = new List<EmployeeExtraPropertyHistory>(); //if (department_Dto.ExtraProperties != curPayrollPeriod.ExtraProperties) //{ // //GeneralInfo oldGeneralInfo = department.GetProperty<GeneralInfo>("generalInfo"); // //List<EmployeeExtraPropertyHistory> physicalIdsHistory = new List<EmployeeExtraPropertyHistory>(); // //var generalInfoPhysicalIdAuditProps = typeof(PhysicalID).GetProperties().Where(x => Attribute.IsDefined(x, typeof(CustomAuditedAttribute))).ToList(); // //List<PhysicalId<Guid>> NewPhysicalIds = generalInfo.PhysicalIds.Where(x => !oldGeneralInfo.PhysicalIds.Any(y => y.Id == x.Id)).ToList(); // //List<PhysicalId<Guid>> UpdatedPhysicalIds = generalInfo.PhysicalIds.Where(x => oldGeneralInfo.PhysicalIds.Any(y => y.Id == x.Id)).ToList(); // //List<PhysicalId<Guid>> DeletedPhysicalIds = oldGeneralInfo.PhysicalIds.Where(x => !generalInfo.PhysicalIds.Any(y => y.Id == x.Id)).ToList(); // //for (int i = 0; i < NewPhysicalIds.Count; i++) // //{ // // PhysicalId<Guid> curPhId = generalInfo.PhysicalIds[i]; // // EmployeeExtraPropertyHistory newPhIdHistory = new EmployeeExtraPropertyHistory(2, "Physical Id", curPhId.IDNumber, "Created"); // // physicalIdsHistory.Add(newPhIdHistory); // //} // //for (int i = 0; i < UpdatedPhysicalIds.Count; i++) // //{ // // PhysicalId<Guid> curPhId = generalInfo.PhysicalIds[i]; // // PhysicalId<Guid> oldPhId = oldGeneralInfo.PhysicalIds.First(x => x.Id == curPhId.Id); // // EmployeeExtraPropertyHistory updatedPhIdHistory = new EmployeeExtraPropertyHistory(2, "Physical Id", curPhId.IDNumber, "Updated"); // // foreach (var prop in generalInfoPhysicalIdAuditProps) // // { // // updatedPhIdHistory.PropertyChanges = new List<EmployeeTypePropertyChange>(); // // EmployeeTypePropertyChange propertyChange = new EmployeeTypePropertyChange(); // // object origVal = prop.GetValue(oldPhId); // // propertyChange.OriginalValue = origVal == null ? "" : origVal.ToString(); // // object newVal = prop.GetValue(curPhId); // // propertyChange.NewValue = newVal == null ? "" : newVal.ToString(); // // if (propertyChange.OriginalValue == propertyChange.NewValue) continue; // // propertyChange.PropertyName = prop.Name; // // if (prop.Name.EndsWith("Id")) // // { // // try // // { // // string valuePropName = prop.Name.TrimEnd('d', 'I'); // // propertyChange.PropertyName = valuePropName; // // var valueProp = typeof(PhysicalID).GetProperty(valuePropName); // // DictionaryValue _origValObj = (DictionaryValue)valueProp.GetValue(oldPhId); // // if (_origValObj == null) _origValObj = await DictionaryValuesRepo.GetAsync((Guid)origVal); // // string _origVal = _origValObj.Value; // // propertyChange.OriginalValue = origVal == null ? "" : _origVal; // // DictionaryValue _newValObj = (DictionaryValue)valueProp.GetValue(curPhId); // // if (_newValObj == null) _newValObj = await DictionaryValuesRepo.GetAsync((Guid)newVal); // // string _newVal = _newValObj.Value; // // propertyChange.NewValue = _newValObj == null ? "" : _newVal; // // } // // catch (Exception ex) // // { // // } // // } // // propertyChange.PropertyTypeFullName = prop.Name.GetType().FullName; // // updatedPhIdHistory.PropertyChanges.Add(propertyChange); // // } // // physicalIdsHistory.Add(updatedPhIdHistory); // //} // //for (int i = 0; i < DeletedPhysicalIds.Count; i++) // //{ // // PhysicalId<Guid> curPhId = generalInfo.PhysicalIds[i]; // // EmployeeExtraPropertyHistory deletedPhIdHistory = new EmployeeExtraPropertyHistory(2, "Physical Id", curPhId.IDNumber, "Deleted"); // // physicalIdsHistory.Add(deletedPhIdHistory); // //} // entityChangeInfo.SetProperty("extraPropertiesHistory", allExtraPropertyHistories); //} #endregion } //curPayrollPeriod.Code = payrollPeriod_Dto.Code; curPayrollPeriod.Name = payrollPeriod_Dto.Name; curPayrollPeriod.PeriodFrequency = payrollPeriod_Dto.PeriodFrequency; curPayrollPeriod.ExtraPeriods = payrollPeriod_Dto.ExtraPeriods; curPayrollPeriod.PeriodStartDate = payrollPeriod_Dto.PeriodStartDate; curPayrollPeriod.PeriodEndDate = payrollPeriod_Dto.PeriodEndDate; #region Child Entities #region Pay Periods if (payrollPeriod_Dto.PayPeriods != null) { //Getting New PS_PayPeriod_Dto[] PayrollPeriods = payrollPeriod_Dto.PayPeriods.ToArray(); //Getting Current int[] curPayrollPeriodsIds = curPayrollPeriod.PayPeriods != null && curPayrollPeriod.PayPeriods.Count > 0 ? curPayrollPeriod.PayPeriods.Select(x => x.Id).ToArray() : new int[0]; List <int> toDeletePayrollPeriods = new List <int>(); //Removing Removed for (int i = 0; i < curPayrollPeriodsIds.Length; i++) { if (!PayrollPeriods.Any(x => x.Id == curPayrollPeriodsIds[i])) { curPayrollPeriod.PayPeriods.Remove(curPayrollPeriod.PayPeriods.First(x => x.Id == curPayrollPeriodsIds[i])); toDeletePayrollPeriods.Add(curPayrollPeriodsIds[i]); } } //Adding & Updating New for (int i = 0; i < PayrollPeriods.Length; i++) { if (!curPayrollPeriod.PayPeriods.Any(x => x.Id == PayrollPeriods[i].Id)) { PayrollPeriods[i].Id = 0; curPayrollPeriod.PayPeriods.Add(ObjectMapper.Map <PS_PayPeriod_Dto, PS_PayPeriod>(PayrollPeriods[i])); } else { var newPeriod = PayrollPeriods[i]; var curPeriod = curPayrollPeriod.PayPeriods.First(x => x.PayrollPeriodId == newPeriod.PayrollPeriodId && x.Id == newPeriod.Id); curPeriod.Name = newPeriod.Name; curPeriod.AttendanceCutOffDate = newPeriod.AttendanceCutOffDate; curPeriod.EmployeeTransactionCutOffDate = newPeriod.EmployeeTransactionCutOffDate; curPeriod.PayrollProcessingDate = newPeriod.PayrollProcessingDate; curPeriod.PayrollReminderIssuanceDays = newPeriod.PayrollReminderIssuanceDays; curPeriod.ApprovalDate = newPeriod.ApprovalDate; curPeriod.ApprovalReminderIssuanceDays = newPeriod.ApprovalReminderIssuanceDays; curPeriod.PaymentDate = newPeriod.PaymentDate; curPeriod.GLExpensePostingDate = newPeriod.GLExpensePostingDate; curPeriod.OffCyclePayroll = newPeriod.OffCyclePayroll; await PS_PayrollPeriodAppService.PayPeriodsRepo.UpdateAsync(curPeriod); } } //Appending Deleted for (int i = 0; i < toDeletePayrollPeriods.Count; i++) { await PS_PayrollPeriodAppService.PayPeriodsRepo.DeleteAsync(x => x.Id == toDeletePayrollPeriods[i]); } } #endregion #endregion var preUpdate = await PS_PayrollPeriodAppService.Repository.UpdateAsync(curPayrollPeriod); var updated = await PS_PayrollPeriodAppService.GetPayrollPeriodAsync(curPayrollPeriod.Id); if (AuditingManager.Current != null) { AuditingManager.Current.Log.EntityChanges.Add(entityChangeInfo); } return(StatusCode(200, updated)); } else { payrollPeriod_Dto.Id = 0; payrollPeriod_Dto.PayPeriods.ForEach(x => { x.Id = 0; }); PS_PayrollPeriod_Dto added = await PS_PayrollPeriodAppService.CreateAsync(payrollPeriod_Dto); added = await PS_PayrollPeriodAppService.GetPayrollPeriodAsync(added.Id); if (AuditingManager.Current != null) { EntityChangeInfo entityChangeInfo = new EntityChangeInfo(); entityChangeInfo.EntityId = added.Id.ToString(); entityChangeInfo.EntityTenantId = added.TenantId; entityChangeInfo.ChangeTime = DateTime.Now; entityChangeInfo.ChangeType = EntityChangeType.Created; entityChangeInfo.EntityTypeFullName = typeof(PS_PayrollPeriod).FullName; AuditingManager.Current.Log.EntityChanges.Add(entityChangeInfo); } return(StatusCode(200, added)); } } } catch (Exception ex) { } } return(StatusCode(500)); }