Exemple #1
0
        public static Dictionary <Int32, Object> WorkoutDeltaForItemUpdate(FinanceDocumentUIViewModel oldDoc, FinanceDocumentUIViewModel newDoc)
        {
            Dictionary <Int32, Object> dictDelta = new Dictionary <Int32, Object>();

            if (oldDoc == null || newDoc == null || Object.ReferenceEquals(oldDoc, newDoc) ||
                oldDoc.ID != newDoc.ID || oldDoc.HID != newDoc.HID || oldDoc.DocType != newDoc.DocType)
            {
                throw new ArgumentException("Invalid inputted parameter Or ID/HID/DocType change is not allowed");
            }
            if (!oldDoc.IsValid() || !newDoc.IsValid())
            {
                throw new Exception("Document is invalid");
            }

            // Items
            Dictionary <Int32, Int32> itemids = new Dictionary <int, Int32>();

            oldDoc.Items.ForEach(o => itemids.Add(o.ItemID, 1));
            newDoc.Items.ForEach(o =>
            {
                if (itemids.ContainsKey(o.ItemID))
                {
                    itemids[o.ItemID] = 2;
                }
                else
                {
                    itemids.Add(o.ItemID, 3);
                }
            });

            // Only left: 1
            // Both: 2
            // Only right: 3
            foreach (var itemid in itemids)
            {
                if (itemid.Value == 1)
                {
                    // ONLY left, DELETE
                    var item = oldDoc.Items.Find(o => o.ItemID == itemid.Key);
                    dictDelta.Add(itemid.Key, null);
                }
                else if (itemid.Value == 2)
                {
                    var item1 = oldDoc.Items.Find(o => o.ItemID == itemid.Key);
                    var item2 = newDoc.Items.Find(o => o.ItemID == itemid.Key);

                    var diffs = FinanceDocumentItemUIViewModel.WorkoutDeltaUpdate(item1, item2);
                    if (diffs.Count > 0)
                    {
                        dictDelta.Add(itemid.Key, diffs);
                    }
                }
                else if (itemid.Value == 3)
                {
                    // Only right, INSERT!
                    var item = newDoc.Items.Find(o => o.ItemID == itemid.Key);
                    dictDelta.Add(itemid.Key, item);
                }
            }

            return(dictDelta);
        }
Exemple #2
0
        public static Dictionary <String, Object> WorkoutDeltaForHeaderUpdate(FinanceDocumentUIViewModel oldDoc,
                                                                              FinanceDocumentUIViewModel newDoc, String usrName)
        {
            Dictionary <String, Object> dictDelta = new Dictionary <string, Object>();

            if (oldDoc == null || newDoc == null || Object.ReferenceEquals(oldDoc, newDoc) ||
                oldDoc.ID != newDoc.ID || oldDoc.HID != newDoc.HID || oldDoc.DocType != newDoc.DocType)
            {
                throw new ArgumentException("Invalid inputted parameter Or ID/HID/DocType change is not allowed");
            }
            if (!oldDoc.IsValid() || !newDoc.IsValid())
            {
                throw new Exception("Document is invalid");
            }

            // Header
            Type t      = typeof(FinanceDocumentUIViewModel);
            Type parent = typeof(BaseViewModel);

            PropertyInfo[] parentProperties = parent.GetProperties();
            Dictionary <String, Object> dictParentProperties = new Dictionary <string, object>();

            foreach (var prop in parentProperties)
            {
                dictParentProperties.Add(prop.Name, null);
            }

            PropertyInfo[] listProperties       = t.GetProperties();
            var            listSortedProperties = listProperties.OrderBy(o => o.Name);

            foreach (PropertyInfo item in listSortedProperties)
            {
                if (dictParentProperties.ContainsKey(item.Name))
                {
                    continue;
                }

                if (item.Name == "ID" || item.Name == "HID" || item.Name == "DocType" ||
                    item.Name == "DocTypeName" || item.Name == "Items" ||
                    item.Name == "TranAmount")
                {
                    continue;
                }

                object oldValue = item.GetValue(oldDoc, null);
                object newValue = item.GetValue(newDoc, null);
                if (item.PropertyType == typeof(Decimal))
                {
                    if (Decimal.Compare((Decimal)oldValue, (Decimal)newValue) != 0)
                    {
                        dictDelta.Add(item.Name, newValue);
                    }
                }
                else if (item.PropertyType == typeof(String))
                {
                    if (String.CompareOrdinal((string)oldValue, (string)newValue) != 0)
                    {
                        dictDelta.Add(item.Name, newValue);
                    }
                }
                else if (item.PropertyType == typeof(DateTime))
                {
                    if (DateTime.Compare(((DateTime)oldValue).Date, ((DateTime)newValue).Date) != 0)
                    {
                        dictDelta.Add(item.Name, newValue);
                    }
                }
                else
                {
                    if (!Object.Equals(oldValue, newValue))
                    {
                        dictDelta.Add(item.Name, newValue);
                    }
                }
            }
            if (dictDelta.Count > 0)
            {
                dictDelta.Add("UpdatedAt", DateTime.Today);
                dictDelta.Add("UpdatedBy", usrName);
            }
            return(dictDelta);
        }