private void GetDownloadUrl(Invoice invoice, PSInvoice psInvoice, string billingAccountName, bool downloadBySubscription = false) { var invoiceDocument = invoice.Documents.FirstOrDefault(p => p.Kind.Equals("INVOICE", StringComparison.InvariantCultureIgnoreCase) && p.Url.Contains(invoice.Name) && p.Source.Equals("DRS", StringComparison.InvariantCultureIgnoreCase)); if (invoiceDocument != null) { DownloadUrl downloadUrl = null; var downloadToken = invoiceDocument.Url.Split('=')?[1].Split('&')?[0]; if (invoiceDocument.Url.ToLowerInvariant().Contains("billingaccounts/default/billingsubscriptions") || downloadBySubscription) { downloadUrl = BillingManagementClient.Invoices .DownloadBillingSubscriptionInvoice( invoiceName: invoice.Name, downloadToken: downloadToken); } else { downloadUrl = BillingManagementClient.Invoices .DownloadInvoice( billingAccountName: billingAccountName, invoiceName: invoice.Name, downloadToken: downloadToken); } psInvoice.DownloadUrl = downloadUrl.Url; psInvoice.DownloadUrlExpiry = downloadUrl.ExpiryTime; } }
public override void ExecuteCmdlet() { try { if (ParameterSetName.Equals(Constants.ParameterSetNames.ListParameterSet)) { string expand = this.GenerateDownloadUrl.IsPresent ? DownloadUrlExpand : null; WriteObject(BillingManagementClient.Invoices.List(expand, null, null, MaxCount).Select(x => new PSInvoice(x)), true); return; } if (ParameterSetName.Equals(Constants.ParameterSetNames.LatestItemParameterSet)) { Invoice invoice = BillingManagementClient.Invoices.GetLatest(); WriteObject(new PSInvoice(invoice)); } else if (ParameterSetName.Equals(Constants.ParameterSetNames.SingleItemParameterSet)) { foreach (var invoiceName in Name) { try { var invoice = new PSInvoice(BillingManagementClient.Invoices.Get(invoiceName)); WriteObject(invoice); } catch (ErrorResponseException error) { WriteWarning(invoiceName + ": " + error.Body.Error.Message); // continue with the next } } } } catch (ErrorResponseException e) { WriteWarning(e.Body.Error.Message); } }
public override void ExecuteCmdlet() { try { var endDate = DateTime.Now; var oneYearSpan = endDate.Subtract(new TimeSpan(365, 0, 0, 0)); var startDate = new DateTime(oneYearSpan.Year, oneYearSpan.Month, 1); if (PeriodStartDate != null && PeriodEndDate != null) { startDate = PeriodStartDate.Value; endDate = PeriodEndDate.Value; } var periodStartDate = startDate.ToString("O"); var periodEndDate = endDate.ToString("O"); IPage <Invoice> invoices = null; if (ParameterSetName.Equals(Constants.ParameterSetNames.ListParameterSet) || ParameterSetName.Equals(Constants.ParameterSetNames.LatestItemParameterSet)) { // modern flow if (!string.IsNullOrWhiteSpace(BillingAccountName)) { if (!string.IsNullOrWhiteSpace(BillingProfileName)) { //fetch by /{ba}/{bp} invoices = BillingManagementClient.Invoices.ListByBillingProfile( billingAccountName: BillingAccountName, billingProfileName: BillingProfileName, periodStartDate: periodStartDate, periodEndDate: periodEndDate); } else // fetch by /{ba} { invoices = BillingManagementClient.Invoices.ListByBillingAccount( billingAccountName: BillingAccountName, periodStartDate: periodStartDate, periodEndDate: periodEndDate); } } else // ba/billingSub/{subId}/invoices { invoices = BillingManagementClient.Invoices.ListByBillingSubscription( periodStartDate: periodStartDate, periodEndDate: periodEndDate); } if (invoices != null && invoices.Any()) { var recentInvoices = (from invoice in invoices where invoice.InvoiceDate.HasValue orderby invoice.InvoiceDate descending select invoice).Take(MaxCount ?? 100); if (ParameterSetName.Equals(Constants.ParameterSetNames.LatestItemParameterSet)) { var psInvoice = new PSInvoice(recentInvoices.FirstOrDefault()); if (GenerateDownloadUrl) { this.GetDownloadUrl( recentInvoices.FirstOrDefault(), psInvoice, BillingAccountName ?? null); } WriteObject(psInvoice); } else { foreach (var invoice in recentInvoices) { var psInvoice = new PSInvoice(invoice); if (GenerateDownloadUrl) { this.GetDownloadUrl( invoice, psInvoice, BillingAccountName ?? null); } WriteObject(psInvoice); } } } return; } if (ParameterSetName.Equals(Constants.ParameterSetNames.SingleItemParameterSet)) { foreach (var invoiceName in Name) { try { var downloadBySubscription = false; Invoice invoice = null; if (!string.IsNullOrWhiteSpace(BillingAccountName)) // modern { invoice = BillingManagementClient.Invoices.Get( billingAccountName: BillingAccountName, invoiceName: invoiceName); } else // legacy { // getbyId retrieves legacy and modern invoices invoice = BillingManagementClient.Invoices.GetById(invoiceName); downloadBySubscription = true; } var psInvoice = new PSInvoice(invoice); if (GenerateDownloadUrl) { this.GetDownloadUrl( invoice, psInvoice, BillingAccountName ?? null, downloadBySubscription); } WriteObject(psInvoice); } catch (ErrorResponseException error) { WriteWarning(invoiceName + ": " + error.Body.Error.Message); // continue with the next } } } } catch (ErrorResponseException e) { WriteWarning(e.Body.Error.Message); } }