Exemplo n.º 1
0
        /// <summary>
        /// Resource price
        /// </summary>
        public ResourcePricing Price(PurchaseOrSalePricingStyleType priceType)
        {
            // find pricing that is ok;
            ResourcePricing price = Apsim.Children(this, typeof(ResourcePricing)).Where(a => a.Enabled & ((a as ResourcePricing).PurchaseOrSale == PurchaseOrSalePricingStyleType.Both | (a as ResourcePricing).PurchaseOrSale == priceType) && (a as ResourcePricing).TimingOK).FirstOrDefault() as ResourcePricing;

            // does simulation have finance
            ResourcesHolder resources       = Apsim.Parent(this, typeof(ResourcesHolder)) as ResourcesHolder;
            bool            financesPresent = (resources.FinanceResource() != null);

            if (price == null)
            {
                if (financesPresent)
                {
                    string warn = "No pricing is available for [r=" + this.Parent.Name + "." + this.Name + "]";
                    if (Clock != null & Apsim.Children(this, typeof(ResourcePricing)).Count > 0)
                    {
                        warn += " in month [" + Clock.Today.ToString("MM yyyy") + "]";
                    }
                    warn += "\nAdd [r=ResourcePricing] component to [r=" + this.Parent.Name + "." + this.Name + "] to include financial transactions for purchases and sales.";

                    if (!Warnings.Exists(warn) & Summary != null)
                    {
                        Summary.WriteWarning(this, warn);
                        Warnings.Add(warn);
                    }
                }
                return(new ResourcePricing()
                {
                    PricePerPacket = 0, PacketSize = 1, UseWholePackets = true
                });
            }
            return(price);
        }
        /// <summary>
        /// Resource price
        /// </summary>
        public ResourcePricing Price(PurchaseOrSalePricingStyleType priceType)
        {
            // find pricing that is ok;
            ResourcePricing price = null;

            // if market exists look for market pricing to override local pricing as all transactions will be through the market
            if (!((this.Parent.Parent as ResourcesHolder).FoundMarket is null) && this.MarketStoreExists)
            {
                price = EquivalentMarketStore.FindAllChildren <ResourcePricing>().FirstOrDefault(a => a.Enabled && ((a as ResourcePricing).PurchaseOrSale == PurchaseOrSalePricingStyleType.Both || (a as ResourcePricing).PurchaseOrSale == priceType) && (a as ResourcePricing).TimingOK);
            }
Exemplo n.º 3
0
 /// <summary>
 /// Convert specified amount of this resource to another value using ResourceType supplied converter
 /// </summary>
 /// <param name="converterName">Name of converter to use</param>
 /// <param name="amount">Amount to convert</param>
 /// <returns>Value to report</returns>
 public object ConvertTo(string converterName, double amount)
 {
     // get converted value
     if (converterName == "$")
     {
         // calculate price as special case using pricing structure if present.
         ResourcePricing price = Price;
         if (price.PricePerPacket > 0)
         {
             double packets = amount / price.PacketSize;
             // this does not include whole packet restriction as needs to report full value
             return(packets * price.PricePerPacket);
         }
         else
         {
             return(null);
         }
     }
     else
     {
         ResourceUnitsConverter converter = Apsim.Children(this, typeof(ResourceUnitsConverter)).Where(a => a.Name.ToLower() == converterName.ToLower()).FirstOrDefault() as ResourceUnitsConverter;
         if (converter != null)
         {
             double result = amount;
             // convert to edible proportion for all HumanFoodStore converters
             // this assumes these are all nutritional. Price will be handled above.
             if (this.GetType() == typeof(HumanFoodStoreType))
             {
                 result *= (this as HumanFoodStoreType).EdibleProportion;
             }
             return(result * converter.Factor);
         }
         else
         {
             string warning = "Unable to find the required unit converter [r=" + converterName + "] in resource [r=" + this.Name + "]";
             Warnings.Add(warning);
             Summary.WriteWarning(this, warning);
             return(null);
         }
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Convert specified amount of this resource to another value using ResourceType supplied converter
 /// </summary>
 /// <param name="converterName">Name of converter to use</param>
 /// <param name="amount">Amount to convert</param>
 /// <returns>Value to report</returns>
 public object ConvertTo(string converterName, double amount)
 {
     // get converted value
     if (converterName == "$")
     {
         // calculate price as special case using pricing structure if present.
         ResourcePricing price = Price;
         if (price.PricePerPacket > 0)
         {
             double packets = amount / price.PacketSize;
             // this does not include whole packet restriction as needs to report full value
             //if(price.UseWholePackets)
             //{
             //    packets = Math.Floor(packets);
             //}
             return(packets * price.PricePerPacket);
         }
         else
         {
             return(null);
         }
     }
     else
     {
         ResourceUnitsConverter converter = Apsim.Children(this, typeof(ResourceUnitsConverter)).Where(a => a.Name.ToLower() == converterName.ToLower()).FirstOrDefault() as ResourceUnitsConverter;
         if (converter != null)
         {
             return(amount * converter.Factor);
         }
         else
         {
             string warning = "Unable to find the required unit converter [r=" + converterName + "] in resource [r=" + this.Name + "]";
             Warnings.Add(warning);
             Summary.WriteWarning(this, warning);
             return(null);
         }
     }
 }