/// <summary> /// The main stock management function that handles a number of events. /// </summary> /// <param name="Model"></param> /// <param name="stockEvent">The event parameters</param> /// <param name="dtToday"></param> /// <param name="fLatitude"></param> public void doStockManagement(TStockList Model, IStockEvent stockEvent, int dtToday = 0, double fLatitude = -35.0) { TCohortsInfo CohortsInfo = new TCohortsInfo(); TPurchaseInfo PurchaseInfo = new TPurchaseInfo(); List<string> sClosed; string sParam; int iParam1; int iParam3; double fValue; int iTag; int iGroups; if (stockEvent != null) { if (stockEvent.GetType() == typeof(TStockAdd)) // add_animals { TStockAdd stockInfo = (TStockAdd)stockEvent; CohortsInfo.sGenotype = stockInfo.genotype; CohortsInfo.iNumber = Math.Max(0, stockInfo.number); if (!ParseRepro(stockInfo.sex, ref CohortsInfo.ReproClass)) throw new Exception("Event ADD does not support sex='" + stockInfo.sex + "'"); if (dtToday > 0) CohortsInfo.iAgeOffsetDays = DaysFromDOY365(stockInfo.birth_day, dtToday); else CohortsInfo.iAgeOffsetDays = 0; CohortsInfo.iMinYears = stockInfo.min_years; CohortsInfo.iMaxYears = stockInfo.max_years; CohortsInfo.fMeanLiveWt = stockInfo.mean_weight; CohortsInfo.fCondScore = stockInfo.cond_score; CohortsInfo.fMeanGFW = stockInfo.mean_fleece_wt; if (dtToday > 0) CohortsInfo.iFleeceDays = DaysFromDOY365(stockInfo.shear_day, dtToday); else CohortsInfo.iFleeceDays = 0; CohortsInfo.sMatedTo = stockInfo.mated_to; CohortsInfo.iDaysPreg = stockInfo.pregnant; CohortsInfo.fFoetuses = stockInfo.foetuses; CohortsInfo.iDaysLact = stockInfo.lactating; CohortsInfo.fOffspring = stockInfo.offspring; CohortsInfo.fOffspringWt = stockInfo.young_wt; CohortsInfo.fOffspringCS = stockInfo.young_cond_score; CohortsInfo.fLambGFW = stockInfo.young_fleece_wt; if (CohortsInfo.iNumber > 0) Model.AddCohorts(CohortsInfo, 1 + DaysFromDOY365(1, dtToday), fLatitude, null); } else if (stockEvent.GetType() == typeof(TStockBuy)) { TStockBuy stockInfo = (TStockBuy)stockEvent; PurchaseInfo.sGenotype = stockInfo.genotype; PurchaseInfo.Number = Math.Max(0, stockInfo.number); if (!ParseRepro(stockInfo.sex, ref PurchaseInfo.Repro)) throw new Exception("Event BUY does not support sex='" + stockInfo.sex + "'"); PurchaseInfo.AgeDays = Convert.ToInt32(Math.Round(MONTH2DAY * stockInfo.age)); // Age in months PurchaseInfo.LiveWt = stockInfo.weight; PurchaseInfo.GFW = stockInfo.fleece_wt; PurchaseInfo.fCondScore = stockInfo.cond_score; PurchaseInfo.sMatedTo = stockInfo.mated_to; PurchaseInfo.Preg = stockInfo.pregnant; PurchaseInfo.Lact = stockInfo.lactating; PurchaseInfo.NYoung = stockInfo.no_young; if ((PurchaseInfo.Preg > 0) || (PurchaseInfo.Lact > 0)) PurchaseInfo.NYoung = Math.Max(1, PurchaseInfo.NYoung); PurchaseInfo.YoungWt = stockInfo.young_wt; if ((PurchaseInfo.Lact == 0) || (PurchaseInfo.YoungWt == 0.0)) // Can't use MISSING as default owing PurchaseInfo.YoungWt = StdMath.DMISSING; // to double-to-single conversion PurchaseInfo.YoungGFW = stockInfo.young_fleece_wt; iTag = stockInfo.usetag; if (PurchaseInfo.Number > 0) { Model.Buy(PurchaseInfo); if (iTag > 0) Model.setTag(Model.Count(), iTag); } } //_ buy _ //sell a number from a group of animals else if (stockEvent.GetType() == typeof(TStockSell)) { TStockSell stockInfo = (TStockSell)stockEvent; Model.Sell(stockInfo.group, stockInfo.number); } //sell a number of animals tagged with a specific tag else if (stockEvent.GetType() == typeof(TStockSellTag)) { TStockSellTag stockInfo = (TStockSellTag)stockEvent; Model.SellTag(stockInfo.tag, stockInfo.number); } else if (stockEvent.GetType() == typeof(TStockShear)) { TStockShear stockInfo = (TStockShear)stockEvent; sParam = stockInfo.sub_group.ToLower(); Model.Shear(stockInfo.group, ((sParam == "adults") || (sParam == "both") || (sParam == "")), ((sParam == "lambs") || (sParam == "both"))); } else if (stockEvent.GetType() == typeof(TStockMove)) { TStockMove stockInfo = (TStockMove)stockEvent; iParam1 = stockInfo.group; if ((iParam1 >= 1) && (iParam1 <= Model.Count())) Model.setInPadd(iParam1, stockInfo.paddock); else throw new Exception("Invalid group number in MOVE event"); } else if (stockEvent.GetType() == typeof(TStockJoin)) { TStockJoin stockInfo = (TStockJoin)stockEvent; Model.Join(stockInfo.group, stockInfo.mate_to, stockInfo.mate_days); } else if (stockEvent.GetType() == typeof(TStockCastrate)) { TStockCastrate stockInfo = (TStockCastrate)stockEvent; Model.Castrate(stockInfo.group, stockInfo.number); } else if (stockEvent.GetType() == typeof(TStockWean)) { TStockWean stockInfo = (TStockWean)stockEvent; iParam1 = stockInfo.group; sParam = stockInfo.sex.ToLower(); iParam3 = stockInfo.number; if (sParam == "males") Model.Wean(iParam1, iParam3, false, true); else if (sParam == "females") Model.Wean(iParam1, iParam3, true, false); else if ((sParam == "all") || (sParam == "")) Model.Wean(iParam1, iParam3, true, true); else throw new Exception("Invalid offspring type \"" + sParam + "\" in WEAN event"); } else if (stockEvent.GetType() == typeof(TStockDryoff)) { TStockDryoff stockInfo = (TStockDryoff)stockEvent; Model.DryOff(stockInfo.group, stockInfo.number); } //split off the requested animals from all groups else if (stockEvent.GetType() == typeof(TStockSplitAll)) { TStockSplitAll stockInfo = (TStockSplitAll)stockEvent; iGroups = Model.Count(); //get pre-split count of groups for (iParam1 = 1; iParam1 <= iGroups; iParam1++) { sParam = stockInfo.type.ToLower(); fValue = stockInfo.value; iTag = stockInfo.othertag; if (sParam == "age") Model.SplitAge(iParam1, Convert.ToInt32(Math.Round(fValue))); else if (sParam == "weight") Model.SplitWeight(iParam1, fValue); else if (sParam == "young") Model.SplitYoung(iParam1); else if (sParam == "number") Model.Split(iParam1, Convert.ToInt32(Math.Round(fValue))); else throw new Exception("Stock: invalid keyword (" + sParam + ") in \"split\" event"); if ((iTag > 0) && (Model.Count() > iGroups)) //if a tag for any new group is given Model.setTag(Model.Count(), iTag); } } //split off the requested animals from one group else if (stockEvent.GetType() == typeof(TStockSplit)) { TStockSplit stockInfo = (TStockSplit)stockEvent; iGroups = Model.Count(); //get pre-split count of groups iParam1 = stockInfo.group; sParam = stockInfo.type.ToLower(); fValue = stockInfo.value; iTag = stockInfo.othertag; if ((iParam1 < 1) && (iParam1 > Model.Count())) throw new Exception("Invalid group number in SPLIT event"); else if (sParam == "age") Model.SplitAge(iParam1, Convert.ToInt32(Math.Round(fValue))); else if (sParam == "weight") Model.SplitWeight(iParam1, fValue); else if (sParam == "young") Model.SplitYoung(iParam1); else if (sParam == "number") Model.Split(iParam1, Convert.ToInt32(Math.Round(fValue))); else throw new Exception("Stock: invalid keyword (" + sParam + ") in \"split\" event"); if ((iTag > 0) && (Model.Count() > iGroups)) //if a tag for the new group is given Model.setTag(Model.Count(), iTag); } else if (stockEvent.GetType() == typeof(TStockTag)) { TStockTag stockInfo = (TStockTag)stockEvent; iParam1 = stockInfo.group; if ((iParam1 >= 1) && (iParam1 <= Model.Count())) Model.setTag(iParam1, stockInfo.value); else throw new Exception("Invalid group number in TAG event"); } else if (stockEvent.GetType() == typeof(TStockSort)) { Model.Sort(); } else if (stockEvent.GetType() == typeof(TStockPrioritise)) { TStockPrioritise stockInfo = (TStockPrioritise)stockEvent; iParam1 = stockInfo.group; if ((iParam1 >= 1) && (iParam1 <= Model.Count())) Model.setPriority(iParam1, stockInfo.value); else throw new Exception("Invalid group number in PRIORITISE event"); } else if (stockEvent.GetType() == typeof(TStockDraft)) { TStockDraft stockInfo = (TStockDraft)stockEvent; sClosed = new List<string>(stockInfo.closed); Model.Draft(sClosed); } else throw new Exception("Event not recognised in STOCK"); } }