In part one, we created a basic Node.js application for inventory tracking that includes routes for Invoice, and Item creation. In this part, we’ve added routes for adding a customer, creating a sales receipt and creating a payment. These can be found in the file routes.js. The routes are as follows:
- Adding a customer: ‘/createCustomer’
- Creating a sales receipt: ‘/createSalesReceipt’
- Creating a payment: ‘/createPayment’
Creating a Customer
To create a customer, we start by rendering a form with some fields. The view for the customer form is /views/createCustomer.ejs
. The following route in routes.js renders the form:
//a route which renders the create a customer form
app.get('/createCustomerForm', function (req, res) {
res.render('createCustomerForm.ejs');
})
From the form, the end user can trigger the createCustomer
route. The createCustomer
form provides the following input fields: Line1, City, Country, SountrySubDivisionCode, PostalCode, CustomerName. These are available in the req.query object. Once the response is returned, either an error page populated by the API error message is rendered.
//a route which creates a new customer object
app.get('/createCustomer', function (req, res) {
//using input from createCustomer form, creating the createCustomer POST body
qbo.createCustomer({
"BillAddr": {
"Line1": req.query.Line1,
"City": req.query.City,
"Country": req.query.Country,
"CountrySubDivisionCode": req.query.CountrySubDivisionCode,
"PostalCode": req.query.PostalCode
},
"Notes": "Here are other details.",
"DisplayName": req.query.CustomerName
}, function (err, customer) {
//if there is an error, render the error page with the error message, else render the createcustomer view
if (err) {
res.render('errorpage.ejs', { errorMessage: err.Fault.Error[0] })
}
else {
res.render('createCustomer.ejs', { displayName: customer.DisplayName, billingAddr: customer.BillAddr, id: customer.Id });
}
})
})
Creating a Sales Receipt
As when creating a customer, we begin by rendering a sales receipt form. To gather the data needed to render this form, we need to make calls to get a list of Customers and Items. We do this by calling the method getCustomersItems from miscFunctions.js. We then pass the data from the response to the view, ‘createSalesReceiptForm.ejs’, and render it.
//a route which renders the create a sales reciept form
app.get('/createSalesReceiptForm', function (req, res) {
miscFunctions.getCustomersItems(qbo);
function renderPage() {
res.render('createSalesReceiptForm.ejs', { locals: { customers: qbo.Customers, items: qbo.Items } });
}
//Add a timeout of 2000 in order to allow the customers and items response to complete before rendering the page
setTimeout(renderPage, 2000);
})
From the form, the end user can trigger the createSalesReceipt route. The createSalesReceipt form provides the following input fields: Description, Quantity (Qty), Item Name, Item Id, and Item Unit Price. We use the JavaScript split function to create an array from ‘itemSelect’. [0]is the item name, [1]is the item id, [2] is the item unit price. These are available in the req.query object. Once the response is returned, either an error page populated by the API error message is rendered.
//a route which calls CreateSalesReciept
app.get('/createSalesReceipt', function (req, res) {
//Check to make sure the front end is sending an item selected, if it is null, render the error page
if (!req.query.itemSelect) {
res.render('errorPage.ejs', { locals: { errorMessage: { Message: 'No Item Selected', Detail: 'You Must Select an Item' } } })
}
else {
// [0]is the item name, [1]is the item id, [2] is the item unit price
var ItemRef = req.query.itemSelect.split('; ');
//building the createSalesReceipt post body
qbo.createSalesReceipt({
"Line": [
{
"Id": "1",
"LineNum": 1,
"Description": req.query.Description,
"Amount": ItemRef[2] * req.query.Qty,
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"ItemRef": {
"value": ItemRef[1],
"name": ItemRef[0]
},
"UnitPrice": ItemRef[2],
"Qty": req.query.Qty,
"TaxCodeRef": {
"value": "NON"
}
}
}
],
"CustomerRef": {
"value": req.query.CustomerId
}
}, function (err, SalesReceipt) {
//render the error page if an error is returned, else, render the salesReciept view
if (err) {
res.render('errorPage.ejs', { locals: { errorMessage: err.Fault.Error[0] } })
} else {
res.render('salesReceipt.ejs', { SalesReceipt: SalesReceipt })
}
})
}
})
Creating a Payment
In this flow, we do not need to create a payment form because it is linked to an invoice. From the invoice summary page, the user can activate the flow for creating a payment by simply clicking a button. Once this happens, the ‘/createPayment’ route goes into action. The inputs are all taken from the Invoice response: CustomerRef, TotalAmt, LineAmt, and InvoiceID. These are available in the req.query object. Once the response is returned, either an error page populated by the API error message is rendered.
//a route which renders the payment form
app.get('/createPayment', function (req, res) {
//using input from createPayment form, creating the createPayment POST body
qbo.createPayment({
"CustomerRef":
{
"value": req.query.CustomerRef
},
"TotalAmt": req.query.TotalAmt,
"Line": [
{
"Amount": req.query.LineAmt,
"LinkedTxn": [
{
"TxnId": req.query.InvoiceId,
"TxnType": "Invoice"
}]
}]
}, function (err, payment) {
if (err) {
res.render('errorPage.ejs', { locals: { errorMessage: err.Fault.Error[0] } })
}
else {
res.render('paymentSuccess.ejs', { Payment: payment})
}
})
})
Leave a Reply