-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonCustomer.js
167 lines (125 loc) · 3.9 KB
/
bamazonCustomer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require("dotenv").config();
var con = require('./sqlConnection.js');
var Item = require("./objects.js");
var inquirer = require("inquirer");
var Table = require('cli-table');
const itemsArr = [];
const LINE = "\n--------------------------------------------------";
const RECEIPT = [];
display_table();
//Display the table of items
function display_table() {
itemsArr.length = 0
console.log("\nWELCOME TO BAMAZON!")
var table = new Table({
head: ['DEPT', 'ID', 'ITEM', 'PRICE', 'QTY']
, colWidths: [15, 10, 20, 10, 10]
, colAligns: [null, null, null, 'right', 'right']
});
con.query("SELECT * FROM vw_itemlist WHERE itm_qty > 0", function (err, results) {
for (var i = 0; i < results.length; i++) {
let itemObj = new Item(results[i].item_id, results[i].itm_name, results[i].itm_cost, results[i].itm_msrp, results[i].itm_prc, results[i].itm_qty);
table.push(
[results[i].dept_name,
itemObj.id,
itemObj.name,
parseFloat(results[i].itm_prc).toFixed(2),
results[i].itm_qty
])
itemsArr.push(itemObj);
}
console.log(table.toString());
setTimeout(select_item, 3000);
})
}
function select_item() {
let array = [];
RECEIPT.length = 0;
//Create list of items with inventory
for (var i = 0; i < itemsArr.length; i++) {
array.push(itemsArr[i].name)
}
inquirer.prompt([
{
name: "item",
message:"Select item to purchase:",
type: "list",
choices: [...array.sort()],
pageSize: 30
}, {
name: "quantity",
type: "number",
message: "How many would you like?",
default: 1
},
{
name: "continue",
type: "confirm",
message: "Would you like to complete the sale?"
}
]).then(function (response) {
if (response.continue === false) {
console.log(LINE + "\nTRANSACTION CANCELLED" + LINE);
select_item();
return;
}
if (isNaN(response.quantity)) {
console.log(LINE + "\nTRANSACTION CANCELLED, INVALID QUANTITY" + LINE);
select_item();
return;
}
let item = findItemByProperty(itemsArr, 'name', response.item)
//Make sure there is enough inventory
if (item.qty >= response.quantity) {
item.qty_selected = response.quantity;
RECEIPT.push(item);
if (response.continue === true) {
console.log("\nCompleting Sale...\n")
con.query("CALL sales_sellItem_sp(?,?)", [item.id, item.qty_selected], function (err, results) {
if (err) {
console.log(err);
}
display_receipt(results[0][0]['LAST_INSERT_ID()']);
});
} else {
display_receipt();
}
} else {
console.log(LINE + "\nSorry, we do not have enough inventory to fill this request." + LINE)
select_item();
}
})
}
function display_receipt(recptNbr) {
let table = new Table({
head: ['ITEM', 'QTY', 'PRICE', 'EXT']
, colWidths: [15, 10, 10, 10]
, colAligns: [null, 'right', 'right', 'right']
, chars: { 'mid': '', 'left-mid': '', 'mid-mid': '', 'right-mid': '' }
});
console.log(LINE);
console.log("Receipt: " + recptNbr);
for (var i = 0; i < RECEIPT.length; i++) {
table.push(
[RECEIPT[i].name,
RECEIPT[i].qty_selected,
parseFloat(RECEIPT[i].price).toFixed(2),
parseFloat(RECEIPT[i].price * RECEIPT[i].qty_selected).toFixed(2)
])
}
console.log(table.toString());
let savings = (RECEIPT[0].qty_selected * RECEIPT[0].msrp) - (RECEIPT[0].qty_selected * RECEIPT[0].price);
if (savings > 0) {
console.log("\nYou have saved " + parseFloat(savings).toFixed(2) + "!");
}
console.log("Thank you for shopping at Bamazon!" + LINE + "\n");
setTimeout(display_table, 4000);
}
function findItemByProperty(array, property, value) {
for (var i = 0; i < array.length; i++) {
if (array[i][property] === value) {
return array[i];
}
}
return [-1];
}