private void OnWFAnimalBuy(object sender, EventArgs e) { RuminantHerd ruminantHerd = Resources.RuminantHerd(); Finance Accounts = Resources.FinanceResource() as Finance; FinanceType bankAccount = Accounts.GetFirst(); var newRequests = ruminantHerd.PurchaseIndividuals.Where(a => a.BreedParams.Breed == BreedName).ToList(); foreach (var newgroup in newRequests.GroupBy(a => a.SaleFlag)) { double fundsAvailable = 100000000; if (bankAccount != null) { fundsAvailable = bankAccount.FundsAvailable; } double cost = 0; foreach (var newind in newgroup) { double value = 0; if (newgroup.Key == Common.HerdChangeReason.SirePurchase) { value = BreedingSirePrice; } else { RuminantValue getvalue = PriceList.Where(a => a.Age < newind.Age).OrderBy(a => a.Age).LastOrDefault(); value = getvalue.PurchaseValue * ((getvalue.Style == Common.PricingStyleType.perKg) ? newind.Weight : 1.0); } if (cost + value <= fundsAvailable) { ruminantHerd.AddRuminant(newind); cost += value; } else { break; } } if (bankAccount != null) { bankAccount.Remove(cost, this.Name, newgroup.Key.ToString()); } } }
private void OnStartOfSimulation(object sender, EventArgs e) { // initialise herd price list PriceList = new List <RuminantValue>(); foreach (RuminantPricing priceGroup in this.Children.Where(a => a.GetType() == typeof(RuminantPricing))) { foreach (RuminantPriceEntry price in priceGroup.Children.Where(a => a.GetType() == typeof(RuminantPriceEntry))) { RuminantValue val = new RuminantValue(); val.Age = price.Age; val.PurchaseValue = price.PurchaseValue; val.Gender = price.Gender; val.Breed = this.BreedName; val.SellValue = price.SellValue; val.Style = priceGroup.PricingStyle; PriceList.Add(val); } } }
private void OnWFAnimalSell(object sender, EventArgs e) { RuminantHerd ruminantHerd = Resources.RuminantHerd(); Finance Accounts = Resources.FinanceResource() as Finance; FinanceType bankAccount = Accounts.GetFirst(); int trucks = 0; double saleValue = 0; double saleWeight = 0; int head = 0; // get current untrucked list of animals flagged for sale List <Ruminant> herd = ruminantHerd.Herd.Where(a => a.SaleFlag != Common.HerdChangeReason.None & a.Breed == BreedName).OrderByDescending(a => a.Weight).ToList(); // if sale herd > min loads before allowing sale if (herd.Select(a => a.Weight / 450.0).Sum() / Number450kgPerTruck >= MinimumTrucksBeforeSelling) { // while truck to fill while (herd.Select(a => a.Weight / 450.0).Sum() / Number450kgPerTruck > MinimumLoadBeforeSelling) { bool nonloaded = true; trucks++; double load450kgs = 0; // while truck below carrying capacity load individuals foreach (var ind in herd) { if (load450kgs + (ind.Weight / 450.0) <= Number450kgPerTruck) { nonloaded = false; head++; load450kgs += ind.Weight / 450.0; RuminantValue getvalue = PriceList.Where(a => a.Age < ind.Age).OrderBy(a => a.Age).LastOrDefault(); saleValue += getvalue.SellValue * ((getvalue.Style == Common.PricingStyleType.perKg) ? ind.Weight : 1.0); saleWeight += ind.Weight; ruminantHerd.RemoveRuminant(ind); } } if (nonloaded) { Summary.WriteWarning(this, String.Format("There was a problem loading the sale truck as sale individuals did not meet the loading criteria for breed {0}", BreedName)); break; } herd = ruminantHerd.Herd.Where(a => a.SaleFlag != Common.HerdChangeReason.None & a.Breed == BreedName).OrderByDescending(a => a.Weight).ToList(); } if (trucks > 0 & bankAccount != null) { // calculate transport costs double transportCost = trucks * DistanceToMarket * CostPerKmTrucking; bankAccount.Remove(transportCost, this.Name, "Transport"); // calculate MLA fees double mlaCost = head * MLAFees; bankAccount.Remove(mlaCost, this.Name, "R&DFee"); // calculate yard fees double yardCost = head * YardFees; bankAccount.Remove(yardCost, this.Name, "YardCosts"); // calculate commission double commissionCost = saleValue * SalesCommission; bankAccount.Remove(commissionCost, this.Name, "SalesCommission"); // add and remove from bank bankAccount.Add(saleValue, this.Name, "Sales"); } } }
private void OnStartOfSimulation(object sender, EventArgs e) { // initialise herd price list PriceList = new List<RuminantValue>(); foreach (RuminantPricing priceGroup in this.Children.Where(a => a.GetType() == typeof(RuminantPricing))) { foreach (RuminantPriceEntry price in priceGroup.Children.Where(a => a.GetType() == typeof(RuminantPriceEntry))) { RuminantValue val = new RuminantValue(); val.Age = price.Age; val.PurchaseValue = price.PurchaseValue; val.Gender = price.Gender; val.Breed = this.BreedName; val.SellValue = price.SellValue; val.Style = priceGroup.PricingStyle; PriceList.Add(val); } } }