Skip to content

Commit

Permalink
V6.0: Add support for tax
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-mous committed Nov 1, 2020
1 parent 54ee445 commit fe09f7d
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 15 deletions.
17 changes: 13 additions & 4 deletions PrinterPiExtension/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,29 @@

try { //Try eBay
let address = document.querySelector("#shipToAddress").innerText;
let shipping = document.querySelectorAll(".BuyerPurchaseDetails--1nhS6")[3].children[1].innerText;
let total = document.querySelectorAll(".BuyerPurchaseDetails--1nhS6")[2].children[1].innerText;
let shipping = document.querySelectorAll(".BuyerPurchaseDetails--1nhS6")[3].children[1].innerText;
shipping = parseFloat(shipping.substring(shipping.indexOf("$")+1)); //Shipping as a float
let grandTotal = document.querySelectorAll(".BuyerPurchaseDetails--1nhS6")[2].children[1].innerText; //Shipping+items+tax
grandTotal = parseFloat(grandTotal.substring(grandTotal.indexOf("$")+1));
let itemTotal = 0; //Total cost of items
let items = document.querySelectorAll(".PurchasedItem---CkHb");
let item_arr = [];
for (let i=0; i<items.length; i++) { //Iterate through the items
let itm = items[i].children[1];
let price = itm.children[2].innerText;
itemTotal += parseFloat(price.substring(price.indexOf("$")+1));
item_arr.push({
desc: itm.children[0].innerText,
sku: "I",
qty: itm.children[1].innerText.slice(5),
price: itm.children[2].innerText
price: price
});
}
chrome.runtime.sendMessage({ //Send the first data
to: address,
shipping: shipping,
subtotal: total,
subtotal: itemTotal,
tax: grandTotal-(shipping+itemTotal),
items: item_arr
});
} catch (errA) {
Expand All @@ -60,6 +66,8 @@
}
}

let tax = 0; //No tax currently

//Get the total and shipping (if any)
let total = 0;
let shipping = 0;
Expand Down Expand Up @@ -95,6 +103,7 @@
chrome.runtime.sendMessage({
to: addr,
shipping: shipping,
tax: tax,
subtotal: total,
items: items_arr
});
Expand Down
34 changes: 26 additions & 8 deletions PrinterPiExtension/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* @typedef {Object} Packet
* @property {string} to Address of the buyer
* @property {string} shipping Shipping paid by buyer
* @property {string} tax Tax paid by buyer
* @property {string} subtotal Subtotal paid by buyer (not including shipping)
* @property {Array<Item>} items Items purchased
* @property {Settings} settings User settings as defined above
Expand All @@ -63,14 +64,15 @@
* @param {string} resError Response error (if any)
*/


const VERSION_NO = 6.0; //Current version

/*
Data format to send to PrinterPi (NOTE: item descs and skus MUST NOT CONTAIN ~ and NO EXTRA SPACES ARE ALLOWED before/after parameters):
To: Line1/n/Line2/n/Line3
From: Line1/n/Line2/n/Line3
Subtotal: $5.00
Tax: $0.00
Shipping: $3.00
SaveFile: 1
Item: Item_1~I123~1~$1.00
Expand Down Expand Up @@ -144,8 +146,9 @@ let convertToMessage = (pkt) => {
To: Line1/n/Line2/n/Line3
From: Line1/n/Line2/n/Line3
Subtotal: $5.00
Shipping: $3.00
Subtotal: $2.00
Tax: $1.00
Shipping: $1.00
Item: Item_1~I123~1~$1.00
Item: Item 2, And 3~I32-+A5~44~$1.00
Message: Contact me at eikyutsuho@gmail.com if you have any questions/concerns/n/Thank you for your business!
Expand All @@ -155,6 +158,7 @@ let convertToMessage = (pkt) => {
msg += "To: " + pkt.to.split("\n").join("/n/") + "\r\n";
msg += "From: " + pkt.settings.from.split("\n").join("/n/") + "\r\n";
msg += "Subtotal: " + pkt.subtotal + "\r\n";
msg += "Tax: " + pkt.tax + "\r\n";
msg += "Shipping: " + pkt.shipping + "\r\n";
pkt.items.forEach((item) => msg += "Item: " + item.desc.replace("~", " ") + "~" + item.sku.replace("~", " ") + "~" + item.qty + "~" + item.price + "\r\n");
msg += "Message: " + pkt.settings.messages.split("\n").join("~") + "\r\n";
Expand All @@ -174,6 +178,7 @@ let sendData = (pkt) => {
done_msg.innerHTML = "Sending data...";
done_msg.classList = "text-warning";
let msg = convertToMessage(pkt);
console.log(msg);
fetch("http://" + pkt.settings.ipAddress, {
method: "POST",
headers: {
Expand Down Expand Up @@ -207,7 +212,7 @@ let sendData = (pkt) => {


/**
* Download the data Packet onto the user's OS (only select options [to, shipping, subtotal and items] are used)
* Download the data Packet onto the user's OS (only select options [to, shipping, tax, subtotal, items and version] are used)
*
* @function downloadFile
* @param {Packet} pkt
Expand All @@ -216,8 +221,10 @@ let downloadFile = (pkt, filepath) => {
let blob = new Blob([JSON.stringify({
to: pkt.to,
shipping: pkt.shipping,
tax: pkt.tax,
subtotal: pkt.subtotal,
items: pkt.items
items: pkt.items,
version: VERSION_NO
})], {type: "application/json"});
let url = URL.createObjectURL(blob);
let item_skus = pkt.items.filter((item) => item.sku.length > 1);
Expand Down Expand Up @@ -315,6 +322,7 @@ let getData = () => { //Read the data from the HTML page
let data = {
to: document.getElementById("Address").value,
subtotal: document.getElementById("Subtotal").value,
tax: document.getElementById("Tax").value,
shipping: document.getElementById("Shipping").value,
items: items
}
Expand Down Expand Up @@ -346,6 +354,7 @@ let getPacket = (settings) => {
let setData = (pkt) => {
document.getElementById("Address").value = pkt.to;
document.getElementById('Shipping').value = pkt.shipping;
document.getElementById('Tax').value = pkt.tax;
document.getElementById('Subtotal').value = pkt.subtotal;
document.getElementById('items').innerHTML = ""; //Remove existing items
pkt.items.forEach((item) => addItem(item));
Expand Down Expand Up @@ -406,8 +415,8 @@ let validateNumberInput = (ele, name) => {
let validateInputs = () => {
let err_msg = ""; //Hold the error to be returned to the user

//Validate the shipping and total
var values = ["Subtotal", "Shipping"];
//Validate the shipping, tax and total
var values = ["Subtotal", "Tax", "Shipping"];
values.forEach((id) => {
let res = validateNumberInput(document.getElementById(id), id)
if (res)
Expand Down Expand Up @@ -539,9 +548,14 @@ let parseFile = (ev) => {
const reader = new FileReader();
reader.onload = (event) => {
const data = JSON.parse(event.target.result);
let tax = 0;
if (data.version >= 6.0) { //Tax support added in version 6.0
tax = data.tax
}
setData({ //Set the data packet
to: data.to,
shipping: data.shipping,
tax: tax,
subtotal: data.subtotal,
items: data.items,
});
Expand Down Expand Up @@ -582,9 +596,12 @@ window.onload = () => { //Add event listeners, etc.
document.getElementById('file-dialog').addEventListener('change', parseFile)
document.getElementById("items-row-btn").addEventListener("click", addRowItems);
document.getElementById("options-btn").addEventListener("click", showOptions);
document.getElementById('Shipping').addEventListener("change", validateInputs);
document.getElementById('Shipping').addEventListener("change", validateInputs);
document.getElementById('Tax').addEventListener("change", validateInputs);
document.getElementById('Subtotal').addEventListener("change", validateInputs);
document.getElementById('Address').addEventListener("change", validateInputs);

$('[data-toggle="tooltip"]').tooltip(); //Setup up tooltips
}

chrome.runtime.onMessage.addListener((msg) => { //Listen for messages and set the data accordingly
Expand All @@ -598,6 +615,7 @@ chrome.runtime.onMessage.addListener((msg) => { //Listen for messages and set th
setData({ //Set the data packet
to: msg.to,
shipping: msg.shipping,
tax: msg.tax,
subtotal: msg.subtotal,
items: msg.items,
});
Expand Down
4 changes: 3 additions & 1 deletion PrinterPiExtension/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ <h5 class="text-center p-0 pt-1 m-0">Address</h5>
<textarea class="form-control h-100" rows="4" id="Address"></textarea>
<h5 class="text-center p-0 pt-1 m-0">Shipping</h5>
<input type="text" class="form-control w-100" id="Shipping" />
<h5 class="text-center p-0 pt-1 m-0">Subtotal<p class="text-muted h6">(not including shipping)</p></h5>
<h5 class="text-center p-0 pt-1 m-0">Tax</h5>
<input type="text" class="form-control w-100" id="Tax" />
<h5 class="text-center p-0 pt-1 m-0 mb-1">Subtotal<button class="btn btn-secondary btn-sm p-0 m-0 mx-1" data-toggle="tooltip" data-html="true" title="<em>Item total not including shipping or tax</em>">&#10068</button></h5>
<input type="text" class="form-control w-100" id="Subtotal" />
<hr class="py-1 mt-2 mb-0">
<h5 class="text-center p-0 pt-1 m-0">Items Purchased</h5>
Expand Down
2 changes: 1 addition & 1 deletion PrinterPiExtension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "PrinterPi",
"version": "5.3",
"version": "6.0",
"description": "Parse eBay Print Shipping Label or PayPal Activity pages and send the data to an application that controls a receipt printer",

"options_page": "options.html",
Expand Down
2 changes: 2 additions & 0 deletions PrinterPiExtension/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<form class="container py-2" style="max-width: 450px" id="settings-form">
<h3 class="title text-center font-weight-bold">PrinterPi Settings</h2>
<hr class="py-1 my-1">
<h6 class="text-center text-muted">Version: 6.0</h6>
<hr class="py-1 my-1">
<h6 class="text-center"><span id="info-msg">Loading...</span></h6>
<hr class="py-1 my-1">
<h5 class="text-center p-0 pt-1 m-0">Return Address</h5>
Expand Down
16 changes: 16 additions & 0 deletions PrinterPiServer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changelog
All notable changes to the PrinterPiServer code will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.0] - 2020-11-01
### Added
- Support for Tax field in transmitted data packets

## [1.0.0] - 2020-09-15
### Added
- Add support for transmitting to and from addresses, as well as custom message
### Changed
- Implemented direct HTTP connections via custom port
- Update way receipts are printed
1 change: 1 addition & 0 deletions PrinterPiServer/Packet.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Packet {
public String[] from;
public double subtotal;
public double shipping;
public double tax;
public double total;
public Set<Item> items;
public String[] messages;
Expand Down
6 changes: 6 additions & 0 deletions PrinterPiServer/Printer.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ public static void print(Packet packet) throws IOException { //String address, S
write(" $" + String.format("%.2f",packet.shipping));
cr();
style(0x88); //Bold, underline
write("Tax:");
style(0x00); //Clear
cr();
write(" $" + String.format("%.2f",packet.tax));
cr();
style(0x88); //Bold, underline
write("Total:");
style(0x00); //Clear
cr();
Expand Down
5 changes: 4 additions & 1 deletion PrinterPiServer/PrinterServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ Data format (NOTE: item descs and skus MUST NOT CONTAIN ~ or ` and NO EXTRA SPAC
case "shipping":
pkt.shipping = Double.parseDouble(value.substring(1));
break;
case "tax":
pkt.tax = Double.parseDouble(value.substring(1));
break;
case "item":
String desc = value.substring(0, value.indexOf("~"));
value = value.substring(value.indexOf("~")+1);
Expand All @@ -129,7 +132,7 @@ Data format (NOTE: item descs and skus MUST NOT CONTAIN ~ or ` and NO EXTRA SPAC
default:
System.out.println("Unrecognized parameter: " + key);
}
pkt.total = pkt.shipping + pkt.subtotal;
pkt.total = pkt.shipping + pkt.subtotal + pkt.tax;
}
if (pkt.isComplete()) {
response = "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n{\"success\":true}\r\n";
Expand Down

0 comments on commit fe09f7d

Please sign in to comment.