private PurchaseOrderHistoryUnit GetPoNewValues(List <AuditEntryProperty> PropertiesList) { PurchaseOrderHistoryUnit poHistory = new PurchaseOrderHistoryUnit(); Type poHistoryType = poHistory.GetType(); foreach (var item in PropertiesList) { PropertyInfo poPropertyInfo = poHistoryType.GetProperty(item.PropertyName); if (item.NewValue.ToString().Length > 0 && CoreHelpers.HasProperty(poHistoryType, item.PropertyName)) { poPropertyInfo.SetValue(poHistory, CoreHelper.CoreHelpers.ChangeType(item.NewValue, poPropertyInfo.PropertyType), null); } } return(poHistory); }
/// <summary> /// Tracking purchaseOrderHistory /// </summary> /// <param name="cancellationToken"></param> /// <param name="accountingItemLog"></param> /// <returns></returns> private async Task CreatePurchaseOrderHistory(CancellationToken cancellationToken, AuditEntry accountingItemLog) { try { var poNewHistory = new PurchaseOrderHistoryUnit(); var poOldHistory = new PurchaseOrderHistoryUnit(); var PropertiesList = accountingItemLog.Properties.FindAll(u => u.PropertyName != "LajitId" && u.PropertyName != "Id"); var propAccountingItemId = accountingItemLog.Properties.Find(u => u.PropertyName == "Id"); var isClose = accountingItemLog.Properties.Find(u => u.PropertyName == "IsClose"); if (accountingItemLog.StateName == AuditEntryState.EntityAdded.ToString()) { poNewHistory = GetPoNewValues(PropertiesList); poNewHistory.AccountingItemId = Convert.ToInt64(propAccountingItemId.NewValue); poNewHistory.ModificationTypeId = ModificationType.Created; PurchaseOrderHistory.Add(poNewHistory); await base.SaveChangesAsync(cancellationToken).ConfigureAwait(false); } else if (accountingItemLog.StateName == AuditEntryState.EntityModified.ToString()) { poNewHistory = GetPoNewValues(PropertiesList); poOldHistory = GetPoOldValues(PropertiesList); poNewHistory.AccountingItemId = Convert.ToInt64(propAccountingItemId.NewValue); poOldHistory.AccountingItemId = Convert.ToInt64(propAccountingItemId.OldValue); //If Job OR Line Changed if (poOldHistory.JobId != poNewHistory.JobId || poOldHistory.AccountId != poNewHistory.AccountId) { poOldHistory.Amount = -poOldHistory.Amount; poOldHistory.ModificationTypeId = ModificationType.Linechange; if (poNewHistory.SourceTypeId != SourceType.PO) { poOldHistory.SourceTypeId = poNewHistory.SourceTypeId; } PurchaseOrderHistory.Add(poOldHistory); await base.SaveChangesAsync(cancellationToken).ConfigureAwait(false); poOldHistory.Amount = Math.Abs(poOldHistory.Amount.Value); poNewHistory.Amount = Math.Abs(poOldHistory.Amount.Value); poNewHistory.ModificationTypeId = ModificationType.Linechange; PurchaseOrderHistory.Add(poNewHistory); await base.SaveChangesAsync(cancellationToken).ConfigureAwait(false); } //if amount Changed poNewHistory = GetPoNewValues(PropertiesList); int value = decimal.Compare(poNewHistory.Amount.Value, poOldHistory.Amount.Value); if (value != 0) { poNewHistory.AccountingItemId = Convert.ToInt64(propAccountingItemId.NewValue); poNewHistory.ChangeInAmount = poNewHistory.Amount.Value - poOldHistory.Amount.Value; poNewHistory.ModificationTypeId = poNewHistory.SourceTypeId == SourceType.PO ? (value > 0 ? ModificationType.IncreasedAmount : ModificationType.DecreasedAmount) : ModificationType.Reduced; PurchaseOrderHistory.Add(poNewHistory); await base.SaveChangesAsync(cancellationToken).ConfigureAwait(false); } //if PO is Closed if (isClose.NewValue.ToString().Length > 0 && Convert.ToBoolean(isClose.NewValue)) { poNewHistory = GetPoNewValues(PropertiesList); poNewHistory.AccountingItemId = Convert.ToInt64(propAccountingItemId.NewValue); poNewHistory.ModificationTypeId = ModificationType.Closed; PurchaseOrderHistory.Add(poNewHistory); await base.SaveChangesAsync(cancellationToken).ConfigureAwait(false); } } else if (accountingItemLog.StateName == AuditEntryState.EntityDeleted.ToString()) { poOldHistory = GetPoOldValues(PropertiesList); poOldHistory.ModificationTypeId = ModificationType.Deleted; poOldHistory.AccountingItemId = Convert.ToInt64(propAccountingItemId.OldValue); PurchaseOrderHistory.Add(poOldHistory); await base.SaveChangesAsync(cancellationToken).ConfigureAwait(false); } } catch (Exception ex) { throw ex; } }