lmc/account/billing_history/billing_history.js

211 lines
5.8 KiB
JavaScript
Raw Normal View History

2020-03-13 23:01:39 -04:00
/*
* This file is part of Linode Manager Classic.
*
* Linode Manager Classic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Linode Manager Classic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Linode Manager Classic. If not, see <https://www.gnu.org/licenses/>.
*/
import { settings, elements, apiGet, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.balance = "balance";
elements.balanceNegative = "balance-negative";
elements.billingTable = "billing-table";
elements.lmcRow = "lmc-tr1";
elements.lmcRowAlt = "lmc-tr2";
elements.loading = "loading";
// Data received from API calls
var data = {};
data.invoices = [];
data.payments = [];
// Static references to UI elements
var ui = {};
ui.balance = {};
ui.billingTable = {};
ui.loading = {};
// Temporary state
var state = {};
state.haveInvoices = false;
state.havePayments = false;
// Creates a row for the billing table
var createBillingRow = function(item, alt)
{
var row = document.createElement("tr");
if (alt)
row.className = elements.lmcRowAlt;
else
row.className = elements.lmcRow;
var dateCell = document.createElement("td");
var date = new Date(item.date + "Z");
dateCell.innerHTML = date.toLocaleDateString();
row.appendChild(dateCell);
var description = document.createElement("td");
var amount = document.createElement("td");
if (item.year) {
// Yearly payment report
var span1 = document.createElement("span");
span1.innerHTML = "Yearly Payments Report (";
var link = document.createElement("a");
link.href = "/account/paymentreceiptyear?year=" + item.year;
link.innerHTML = item.year;
var span2 = document.createElement("span");
span2.innerHTML = ")";
description.appendChild(span1);
description.appendChild(link);
description.appendChild(span2);
} else if (item.label) {
// Invoice
var link = document.createElement("a");
link.href = "/account/invoice?iid=" + item.id;
link.innerHTML = item.label;
description.appendChild(link);
if (item.total < 0)
amount.innerHTML = "($" + (-item.total).toFixed(2) + ")";
else
amount.innerHTML = "$" + item.total.toFixed(2);
} else {
// Payment
var link = document.createElement("a");
link.href = "/account/paymentreceipt?pid=" + item.id;
link.innerHTML = "Payment";
var thanks = document.createElement("span");
thanks.innerHTML = " - Thank you";
description.appendChild(link);
description.appendChild(thanks);
if (item.usd > 0)
amount.innerHTML = "($" + item.usd.toFixed(2) + ")";
else
amount.innerHTML = "$" + (-item.usd).toFixed(2);
}
row.appendChild(description);
row.appendChild(amount);
return row;
};
// Populate billing table with invoices and payments
var displayAll = function()
{
// Remove loading row
ui.loading.remove();
// Combine to single array
var combined = data.invoices.concat(data.payments);
// Insert yearly payment reports
var now = new Date();
var lowestYear = now.getFullYear();
for (var i = 0; i < combined.length; i++) {
var date = new Date(combined[i].date + "Z");
if (date.getFullYear() < lowestYear)
lowestYear = date.getFullYear();
}
for (var i = now.getFullYear(); i >= lowestYear; i--) {
combined.push({
"date": i + "-01-01T12:00:00",
"year": i
});
}
// Sort
combined.sort(function(a, b)
{
var aDate = new Date(a.date + "Z");
var bDate = new Date(b.date + "Z");
return bDate - aDate;
});
// Insert
for (var i = 0; i < combined.length; i++)
ui.billingTable.appendChild(createBillingRow(combined[i], ui.billingTable.children.length % 2));
};
// Callback for account details API call
var displayBalance = function(response)
{
ui.balance.innerHTML = "$" + response.balance.toFixed(2);
if (response.balance < 0) {
ui.balance.innerHTML += " credit";
} else if (response.balance > 0) {
ui.balance.innerHTML += " outstanding";
ui.balance.className = elements.balanceNegative;
}
};
// Callback for invoices API call
var displayInvoices = function(response)
{
data.invoices = data.invoices.concat(response.data);
// Request the next page if there are more
if (response.page != response.pages) {
apiGet("/account/invoices?page=" + (response.page + 1), displayInvoices, null);
return;
}
state.haveInvoices = true;
if (state.havePayments)
displayAll();
};
// Callback for payments API call
var displayPayments = function(response)
{
data.payments = data.payments.concat(response.data);
// Request the next page if there are more
if (response.page != response.pages) {
apiGet("/account/payments?page=" + (response.page + 1), displayPayments, null);
return;
}
state.havePayments = true;
if (state.haveInvoices)
displayAll();
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.balance = document.getElementById(elements.balance);
ui.billingTable = document.getElementById(elements.billingTable);
ui.loading = document.getElementById(elements.loading);
// Get data from API
apiGet("/account", displayBalance, null);
apiGet("/account/invoices", displayInvoices, null);
apiGet("/account/payments", displayPayments, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();