public void Set(MaterialCommodityData.CatType cat, string fdname, int num, double price) { MaterialCommodities mc = GetNewCopyOf(cat, fdname); mc.Count = num; if (price > 0) { mc.Price = price; } }
public void Set(MaterialCommodityData.CatType cat, string fdname, int num, double price) { MaterialCommodities mc = GetNewCopyOf(cat, fdname); mc.Count = num; if (price > 0) { mc.Price = price; } //log.WriteLine("MC Set:" + cat + " " + fdname + " " + num + " " + mc.count); }
// ignore cat is only used if you don't know what it is public void Change(MaterialCommodityData.CatType cat, string fdname, int num, long price, bool ignorecatonsearch = false) { MaterialCommodities mc = GetNewCopyOf(cat, fdname, ignorecatonsearch); double costprev = mc.Count * mc.Price; double costnew = num * price; mc.Count = Math.Max(mc.Count + num, 0); if (mc.Count > 0 && num > 0) // if bought (defensive with mc.count) { mc.Price = (costprev + costnew) / mc.Count; // price is now a combination of the current cost and the new cost. in case we buy in tranches } //log.WriteLine("MC Change:" + cat + " " + fdname + " " + num + " " + mc.count); }
// ignore cat is only used if you don't know what it is public void Change(DateTime utc, MaterialCommodityData.CatType cat, string fdname, int num, long price, bool ignorecatonsearch = false) { MaterialCommodities mc = GetNewCopyOf(cat, fdname, ignorecatonsearch); double costprev = mc.Count * mc.Price; double costnew = num * price; //if (mc.Count == 0 && num < 0) System.Diagnostics.Debug.WriteLine("{0} Error, removing {1} {2} but nothing counted", utc, fdname, num); mc.Count = Math.Max(mc.Count + num, 0); if (mc.Count > 0 && num > 0) // if bought (defensive with mc.count) { mc.Price = (costprev + costnew) / mc.Count; // price is now a combination of the current cost and the new cost. in case we buy in tranches } //System.Diagnostics.Debug.WriteLine("In {0} At {1} MC Change {2} {3} {4} = {5}", System.Threading.Thread.CurrentThread.Name, utc, cat, fdname, num,mc.Count); }
// ifnorecatonsearch is used if you don't know if its a material or commodity.. for future use. private MaterialCommodities GetNewCopyOf(MaterialCommodityData.CatType cat, string fdname, bool ignorecatonsearch = false) { int index = List.FindIndex(x => x.Details.FDName.Equals(fdname, StringComparison.InvariantCultureIgnoreCase) && (ignorecatonsearch || x.Details.Category == cat)); if (index >= 0) { List[index] = new MaterialCommodities(List[index]); // fresh copy.. return(List[index]); } else { MaterialCommodityData mcdb = MaterialCommodityData.EnsurePresent(cat, fdname); // get a MCDB of this MaterialCommodities mc = new MaterialCommodities(mcdb); // make a new entry List.Add(mc); return(mc); } }