-
Notifications
You must be signed in to change notification settings - Fork 12
/
sellerscript.js
189 lines (160 loc) · 5.57 KB
/
sellerscript.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
web3 = new Web3(web3.currentProvider);
var contract = new web3.eth.Contract(abi, address);
console.log("blockchain connected")
$(document).ready(function () {
$("#_updatebtn").hide();
$("#_addbtn").hide();
// Show / hide register section.
web3.eth.getAccounts().then(function (accounts) {
var account = accounts[0];
contract.methods.isRegistered(account).call().then(function (flag) {
console.log("isRegistered : " + flag);
if (flag) {
$("#_regdiv").hide();
$("#_addbtn").show();
}
});
});
// Fetching products.
web3.eth.getAccounts().then(function (accounts) {
var account = accounts[0];
// Total my product
contract.methods.getTotalProduct(account).call().then(function (totalProduct) {
console.log("totalProduct my : " + totalProduct);
$("#_totalproduct").html(totalProduct);
});
// Fetch my products.
contract.methods.getTotalProduct().call().then(function (totalProduct) {
console.log("totalProduct (global): " + totalProduct);
var index = 1;
for (index = 1; index <= totalProduct; index++) {
contract.methods.getNextProduct(account, index).call().then(function (productDetails) {
// index = productDetails + 1;
console.log(productDetails);
if (productDetails[4]) {
var row = "<tr><th>" + productDetails[0] + "</th><td>" + productDetails[3] + "</td><td>" + productDetails[1] + "</td><td>" + productDetails[2] + "</td><td><button type=\"button\" class=\"btn btn-secondary btn-sm\" onclick=\"priceUpdate(" + productDetails[0] + ")\">Change price</button></td></tr>";
$("#_myproduct_table").find('tbody').append(row);
}
});
}
});
// Fetching total number of my product.
contract.methods.getMyTotalOrder(account).call().then(function (totalOrder) {
console.log("totalOrder my : " + totalOrder);
$("#_total_order").html(totalOrder);
});
// Fetch my orders.
contract.methods.getTotalOrder().call().then(function (totalOrder) {
console.log("totalOrder (global): " + totalOrder);
var index = 1;
for (index = 1; index <= totalOrder; index++) {
contract.methods.getMyNextOrderById(account, index).call().then(function (orderDetails) {
// index = orderDetails + 1;
console.log(orderDetails);
if (orderDetails[6]) {
var row = "<tr><th>" + orderDetails[0] + "</th><td>" + orderDetails[1] + "</td><td>" + orderDetails[2] + "</td><td>" + orderDetails[3] + "</td><td>" + orderDetails[5] + "</td><td>" + orderDetails[4] + "</td><td><button type=\"button\" class=\"btn btn-primary btn-sm\" onclick=\"delivered(" + orderDetails[0] + ")\">Delivered</button>\n<button type=\"button\" class=\"btn btn-secondary btn-sm\" onclick=\"reject(" + orderDetails[0] + ")\">Reject</button></td></tr>";
$("#_order_table").find('tbody').append(row);
}
});
}
});
});
// Register new producer.
$("#_regbutton").click(function () {
web3.eth.getAccounts().then(function (accounts) {
var account = accounts[0];
var name = $("#_regname").val();
return contract.methods.reginsterProducer(name).send({ from: account });
}).then(function (trx) {
console.log(trx);
if (trx.status) {
$("#_regdiv").hide();
$("#_addbtn").show();
}
});
});
// Adding new product.
$("#_addbtn").click(function () {
web3.eth.getAccounts().then(function (accounts) {
var account = accounts[0];
var pname = $("#_pname").val();
var pprice = $("#_price").val();
var pquantity = $("#_pquantity").val();
console.log(pname + pprice + pquantity);
return contract.methods.addProduct(pname, pprice, pquantity).send({ from: account });
}).then(function (trx) {
console.log(trx);
if (trx.status) {
alert("Product added!");
$("#_pname").val("");
$("#_price").val("");
$("#_pquantity").val("");
location.reload();
}
});
});
// Update the price of a product.
$("#_updatebtn").click(function () {
web3.eth.getAccounts().then(function (accounts) {
var account = accounts[0];
var pid = $("#_pname").val();
var pprice = $("#_price").val();
console.log("update button click " + pid + pprice);
return contract.methods.updatePrice(pid, pprice).send({ from: account });
}).then(function (trx) {
console.log(trx);
if (trx.status) {
alert("Price updated!");
$("#_nameidlabel").html("Name");
$("#_pricelabel").html("Price");
$("#_quantitylabel").show();
$("#_addbtn").show();
$("#_updatebtn").hide();
$("#_pname").val('');
$("#_price").val('');
location.reload();
}
});
});
});
// Reject order.
function reject(orderId) {
console.log("Reject " + orderId);
web3.eth.getAccounts().then(function (accounts) {
var account = accounts[0];
var status = "Rejected";
return contract.methods.updateOrderStatus(orderId, status).send({ from: account });
}).then(function (trx) {
console.log(trx);
if (trx.status) {
alert("Order rejected!");
location.reload();
}
});
}
// Delivered order.
function delivered(orderId) {
console.log("delivered " + orderId);
web3.eth.getAccounts().then(function (accounts) {
var account = accounts[0];
var status = "Delivered";
return contract.methods.updateOrderStatus(orderId, status).send({ from: account });
}).then(function (trx) {
console.log(trx);
if (trx.status) {
alert("Order delivered!");
location.reload();
}
});
}
// Add the product details in from for price update.
function priceUpdate(productId) {
console.log("order click : " + productId);
// alert(productId);
$("#_nameidlabel").html("Product ID");
$("#_pricelabel").html("New price");
$("#_quantitylabel").hide();
$("#_addbtn").hide();
$("#_updatebtn").show();
$("#_pname").val(productId);
}