Esempio n. 1
0
        /// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The <see cref="JsonReader" /> to read from.</param>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="existingValue">The existing value of object being read.</param>
        /// <param name="serializer">The calling serializer.</param>
        /// <returns>The object value.</returns>
        /// <exception cref="ArgumentNullException">
        /// reader
        /// or
        /// objectType
        /// </exception>
        /// <exception cref="JsonSerializationException">
        /// </exception>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            BillingProvider billingProvider;
            JObject         json;
            string          billingProviderValue;
            string          invoiceLineItemType;
            InvoiceLineItem lineItem = null;

            json = JObject.Load(reader);

            billingProviderValue = json["billingProvider"].ToString();
            billingProvider      = JsonConvert.DeserializeObject <BillingProvider>($"'{billingProviderValue}'");

            invoiceLineItemType = json["invoiceLineItemType"].ToString();

            if (!invoiceLineItemType.Equals("usage_line_items", StringComparison.InvariantCultureIgnoreCase))
            {
                if (invoiceLineItemType.Equals("billing_line_items", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (billingProvider == BillingProvider.Azure)
                    {
                        lineItem = new UsageBasedLineItem();
                    }
                    else if (billingProvider == BillingProvider.Office)
                    {
                        lineItem = new LicenseBasedLineItem();
                    }
                    else if (billingProvider == BillingProvider.OneTime)
                    {
                        lineItem = new OneTimeInvoiceLineItem();
                    }
                    else if (billingProvider == BillingProvider.All)
                    {
                        lineItem = new OneTimeInvoiceLineItem();
                    }
                }
            }
            else
            {
                if (billingProvider == BillingProvider.Azure)
                {
                    lineItem = new DailyUsageLineItem();
                }
                else if (billingProvider == BillingProvider.Marketplace)
                {
                    lineItem = new DailyRatedUsageLineItem();
                }
            }

            if (lineItem == null)
            {
                throw new JsonSerializationException($"InvoiceLineItemConverter cannot deserialize invoice line items with type: '{invoiceLineItemType}' && billing provider: '{billingProviderValue}'");
            }

            serializer.Populate(json.CreateReader(), lineItem);

            return(lineItem);
        }
        /// <summary>
        /// Converts an object of type CspUsageLineItem from the input InvoiceLineItem values.
        /// </summary>
        /// <param name="lineItem">InvoiceLineItem object which needs to be converted.</param>
        /// <returns>Converted CspUsageLineItem.</returns>
        private CspUsageLineItem GetSingleInvoiceData(InvoiceLineItem lineItem)
        {
            try
            {
                CspUsageLineItem   cspUsageItem = new CspUsageLineItem();
                DailyUsageLineItem lineItemWithBaseProperties = (DailyUsageLineItem)lineItem;

                cspUsageItem.BillingProvider  = lineItemWithBaseProperties.BillingProvider.ToString();
                cspUsageItem.ChargeEndDate    = lineItemWithBaseProperties.ChargeEndDate;
                cspUsageItem.ChargeStartDate  = lineItemWithBaseProperties.ChargeStartDate;
                cspUsageItem.ConsumedQuantity =
                    float.Parse(
                        lineItemWithBaseProperties.ConsumedQuantity.ToString(CultureInfo.InvariantCulture),
                        CultureInfo.InvariantCulture);
                cspUsageItem.CustomerBillableAccount = lineItemWithBaseProperties.CustomerBillableAccount;
                cspUsageItem.CustomerCompanyName     = lineItemWithBaseProperties.CustomerCompanyName;
                cspUsageItem.invoiceNumber           = lineItemWithBaseProperties.InvoiceNumber;
                cspUsageItem.MpnId   = lineItemWithBaseProperties.MpnId.ToString(CultureInfo.InvariantCulture);
                cspUsageItem.orderId = lineItemWithBaseProperties.OrderId;
                cspUsageItem.PartnerBillableAccountId = lineItemWithBaseProperties.PartnerBillableAccountId;
                cspUsageItem.PartnerId               = lineItemWithBaseProperties.PartnerId;
                cspUsageItem.PartnerName             = lineItemWithBaseProperties.PartnerName;
                cspUsageItem.Region                  = lineItemWithBaseProperties.Region;
                cspUsageItem.ResourceGuid            = lineItemWithBaseProperties.ResourceGuid;
                cspUsageItem.ResourceName            = lineItemWithBaseProperties.ResourceName;
                cspUsageItem.ServiceName             = lineItemWithBaseProperties.ServiceName;
                cspUsageItem.ServiceType             = lineItemWithBaseProperties.ServiceType;
                cspUsageItem.SubscriptionDescription = lineItemWithBaseProperties.SubscriptionDescription;
                cspUsageItem.SubscriptionId          = lineItemWithBaseProperties.SubscriptionId;
                cspUsageItem.SubscriptionName        = lineItemWithBaseProperties.SubscriptionName;
                cspUsageItem.TierMpnId               = lineItemWithBaseProperties.Tier2MpnId.ToString(CultureInfo.InvariantCulture);
                cspUsageItem.UsageDate               = lineItemWithBaseProperties.UsageDate;

                return(cspUsageItem);
            }
            catch
            {
                throw;
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="PSDailyUsageLineItem" /> class.
 /// </summary>
 /// <param name="lineItem">The base line item for this instance.</param>
 public PSDailyUsageLineItem(DailyUsageLineItem lineItem)
 {
     this.CopyFrom(lineItem);
 }