lmc/account/invoice/invoice.js

152 lines
4.6 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.invoiceBody = "invoice-body";
elements.invoiceID = "invoice-id";
elements.lmcRow = "lmc-tr1";
elements.lmcRowAlt = "lmc-tr2";
elements.subnav = "subnav-link";
elements.subnavActive = "subnav-link-active";
elements.subtotal = "subtotal";
elements.tax = "tax";
elements.total = "total";
// Data received from API calls
var data = {};
// Static references to UI elements
var ui = {};
ui.invoiceBody = {};
ui.invoiceID = {};
ui.subtotal = {};
ui.tax = {};
ui.total = {};
// Generates an invoice item table row
var createItemRow = function(item, alt)
{
var row = document.createElement("tr");
if (alt)
row.className = elements.lmcRowAlt;
else
row.className = elements.lmcRow;
var description = document.createElement("td");
description.innerHTML = item.label;
row.appendChild(description);
var from = document.createElement("td");
var fromDate = new Date(item.from + "Z");
from.innerHTML = fromDate.toLocaleString();
row.appendChild(from);
var to = document.createElement("td");
var toDate = new Date(item.to + "Z");
to.innerHTML = toDate.toLocaleString();
row.appendChild(to);
var quantity = document.createElement("td");
quantity.innerHTML = item.quantity;
row.appendChild(quantity);
var unitPrice = document.createElement("td");
unitPrice.innerHTML = "$" + parseFloat(item.unit_price).toFixed(4);
row.appendChild(unitPrice);
var subtotal = document.createElement("td");
subtotal.innerHTML = "$" + item.amount.toFixed(2);
row.appendChild(subtotal);
var tax = document.createElement("td");
tax.innerHTML = "$" + item.tax.toFixed(2);
row.appendChild(tax);
var total = document.createElement("td");
total.innerHTML = "$" + item.total.toFixed(2);
row.appendChild(total);
return row;
};
// Callback for invoice API call
var displayInvoice = function(response)
{
ui.subtotal.innerHTML = "$" + response.subtotal.toFixed(2);
ui.tax.innerHTML = "$" + response.tax.toFixed(2);
ui.total.innerHTML = "$" + response.total.toFixed(2);
};
// Callback for invoice items API call
var displayItems = function(response)
{
// Insert invoice items into table
for (var i = 0; i < response.data.length; i++)
ui.invoiceBody.appendChild(createItemRow(response.data[i], ui.invoiceBody.children.length % 2));
// Request the next page if there are more
if (response.page != response.pages)
apiGet("/account/invoices/" + data.params.iid + "/items?page=" + (response.page + 1), displayItems, null);
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
// We need an invoice ID, so die if we don't have it
if (!data.params.iid) {
alert("No invoice ID supplied!");
return;
}
setupHeader();
// Highlight the remote access subnav link
var subnavLinks = document.getElementsByClassName(elements.subnav);
for (var i = 0; i < subnavLinks.length; i++) {
if (subnavLinks[i].pathname == "/account/billing_history")
subnavLinks[i].className = elements.subnav + " " + elements.subnavActive;
else
subnavLinks[i].className = elements.subnav;
}
// Get element references
ui.invoiceBody = document.getElementById(elements.invoiceBody);
ui.invoiceID = document.getElementById(elements.invoiceID);
ui.subtotal = document.getElementById(elements.subtotal);
ui.tax = document.getElementById(elements.tax);
ui.total = document.getElementById(elements.total);
// Set title and table header
document.title += " " + data.params.iid;
ui.invoiceID.innerHTML = data.params.iid;
// Get data from API
apiGet("/account/invoices/" + data.params.iid, displayInvoice, null);
apiGet("/account/invoices/" + data.params.iid + "/items", displayItems, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();