-
Notifications
You must be signed in to change notification settings - Fork 4
/
openFarming.sol
362 lines (302 loc) · 11.6 KB
/
openFarming.sol
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
pragma solidity ^0.4.0;
contract openFarming{
uint[] public userIdList;
mapping (uint => uint) public userIsPresent;
uint public userCount;
uint public cropCount;
uint public storeCount;
uint public transportCount;
uint public buyerOrderCount;
uint public storeOrderCount;
uint public transportOrderCount;
struct userDetails{
string name;
string contact;
uint userid;
uint usertype;
string location;
string email;
string pass;
uint balance;
address userAddress;
}
struct crop{
uint id;
string name;
uint ownId;
uint quantity;
uint price;
uint amountSold;
}
struct store{
uint id;
uint ownId;
uint capacity;
uint price;
uint remain;
}
struct transport{
uint id;
uint ownId;
uint capacity;
uint price;
}
struct buyerOrder{
uint orderId;
uint cropId;
uint buyerId;
uint transportId; //////////to be filled by farmer
uint transportOrderId; //////////to be filled by farmer
uint storeId; //////////to be filled by farmer
uint quantity;
uint statusOrder; ///// 0 -- store ;; 1 -- onWay ;; 2 -- delivered;
uint delivered; ///// 1 -- confirmation
uint picked;
uint accept; //////////////0--decline ;; 1--Accepted ;; 2--No Action
}
struct storeOrder{
uint orderId;
uint cropId;
uint storeId;
uint transportId; //////////to be filled by farmer
uint transportOrderId; //////////to be filled by farmer
uint quantity;
uint statusOrder; ///// 0 -- farmer ;; 1 -- onWay ;; 2 -- delivered;
uint delivered;
uint picked;
uint accept; //////////////0--decline ;; 1--Accepted ;; 2--No Action
}
struct transportOrder{
uint orderId;
uint orderType; ////////////0 -- buyerOrder ;; 1 -- storeOrder
uint orderTaken;
uint transportId;
uint senderId;
uint recieverId;
uint quantity;
uint statusOrder; //////// 0 -- sender ;; 1 -- onWay ;; 2 -- reciever;
uint delivered;
uint picked;
uint accept; //////////////0--decline ;; 1--Accepted ;; 2--No Action
}
mapping ( string => uint) emailToId;
mapping ( uint => userDetails ) public userInfo;
mapping ( uint => crop ) public cropInfo;
mapping ( uint => store ) public storeInfo;
mapping ( uint => transport ) public transportInfo;
mapping ( uint => buyerOrder ) public buyerOrderInfo;
mapping ( uint => storeOrder ) public storeOrderInfo;
mapping ( uint => transportOrder ) public transportOrderInfo;
function getUserId(string email) returns (uint) {
return emailToId[email];
}
function getUserName(uint id) returns (string) {
return userInfo[id].name;
}
function registerMe(string name,string contact,uint userid,uint usertype,string location,string email,string pass) public returns(bool){
if(userIsPresent[userid] != 1)
{
userIsPresent[userid]=1;
userIdList.push(userid);
address adr=msg.sender;
uint bal = 100;
emailToId[email] = userid;
userInfo[userid] = userDetails(name,contact,userid,usertype,location,email,pass,bal,adr);
userCount+=1;
return true;
}
return false;
}
function sendPass(uint id) returns (string){
return userInfo[id].pass;
/*for(uint i = 0; i<userCount; i++){
if(userInfo[i].userid == id ){
return userInfo[i].pass;
}
}*/
}
function addCrop(string name,uint ownid, uint quantity,uint price) returns (bool) {
uint amt = 0;
cropInfo[cropCount] = crop(cropCount,name,ownid,quantity,price,amt);
cropCount++;
return true;
}
function addStore(uint ownid, uint capacity,uint price) returns (bool) {
storeInfo[storeCount] = store(storeCount,ownid,capacity,price,capacity);
storeCount++;
return true;
}
function addTransport(uint ownid, uint capacity,uint price) returns (bool) {
transportInfo[transportCount] = transport(transportCount,ownid,capacity,price);
transportCount++;
return true;
}
/*function addOrder(uint quantity, uint id1, uint id2, uint id3, uint orderType) returns (bool) {
uint st = 0; uint a = 2;
orderInfo[orderCount] = order(orderCount, quantity, id1, id2, id3, orderType, st);
orderCount++;
return true;
}*/
function addBuyerOrder(uint cropId, uint buyerId, uint quant) returns (bool) {
uint trans = 20;
uint store = 20;
uint transOrder = 100;
uint st = 0; uint d = 0; uint p = 0; uint a = 2;
buyerOrderInfo[buyerOrderCount] = buyerOrder(buyerOrderCount, cropId, buyerId, trans, transOrder, store, quant, st, d, p, a);
buyerOrderCount++;
return true;
}
function addStoreOrder(uint cropId, uint storeId, uint quant ) returns (bool) {
uint trans = 20;
uint transOrder = 100;
uint st = 0; uint d = 0; uint p = 0; uint a = 2;
storeOrderInfo[storeOrderCount] = storeOrder(storeOrderCount, cropId, storeId, trans, transOrder, quant, st, d, p, a);
storeOrderCount++;
return true;
}
function addTransportOrder(uint orderType, uint orderTaken, uint transportId ) returns (bool) {
//s r q
uint c;
uint s;
uint r;
uint q;
if(orderType == 0) { /////////////////Buyer
c = buyerOrderInfo[orderTaken].cropId;
s = cropInfo[c].ownId;
r = buyerOrderInfo[orderTaken].buyerId;
q = buyerOrderInfo[orderTaken].quantity;
buyerOrderInfo[orderTaken].transportId = transportId;
buyerOrderInfo[orderTaken].transportOrderId = transportOrderCount;
}
else if(orderType == 1) { /////////////////Storage
c = storeOrderInfo[orderTaken].cropId;
s = cropInfo[c].ownId;
r = storeOrderInfo[orderTaken].storeId;
q = storeOrderInfo[orderTaken].quantity;
storeOrderInfo[orderTaken].transportId = transportId;
storeOrderInfo[orderTaken].transportOrderId = transportOrderCount;
}
uint st = 0; uint d = 0; uint p = 0; uint a = 2;
transportOrderInfo[transportOrderCount] = transportOrder(transportOrderCount, orderType, orderTaken, transportId, s, r, q, st, d, p ,a);
transportOrderCount++;
return true;
}
function changeStatus(uint orderType, uint orderId, uint newStatus) returns (bool) { ////orderType: 0--buyer 1--store 2--transport
if(orderType == 0){
buyerOrderInfo[orderId].statusOrder = newStatus;
}
else if(orderType == 1){
storeOrderInfo[orderId].statusOrder = newStatus;
}
else if(orderType == 2){
transportOrderInfo[orderId].statusOrder = newStatus;
}
}
function changeDelivered(uint orderType, uint orderId, uint newStatus) returns (bool) { ////orderType: 0--buyer 1--store 2--transport
uint transOrderId;
uint s;
uint r;
uint m;
if(orderType == 0){
buyerOrderInfo[orderId].delivered = newStatus;
s = cropInfo[buyerOrderInfo[orderId].cropId].ownId;
r = transportInfo[buyerOrderInfo[orderId].transportId].ownId;
m = transportInfo[buyerOrderInfo[orderId].transportId].price;
userInfo[s].balance -= m;
userInfo[r].balance += m;
transOrderId = buyerOrderInfo[orderId].transportOrderId;
if(transportOrderInfo[transOrderId].delivered == 1){
transportOrderInfo[transOrderId].statusOrder = 2;
buyerOrderInfo[orderId].statusOrder = 2;
}
}
else if(orderType == 1){
storeOrderInfo[orderId].delivered = newStatus;
s = cropInfo[storeOrderInfo[orderId].cropId].ownId;
r = transportInfo[storeOrderInfo[orderId].transportId].ownId;
m = transportInfo[storeOrderInfo[orderId].transportId].price;
userInfo[s].balance -= m;
userInfo[r].balance += m;
transOrderId = storeOrderInfo[orderId].transportOrderId;
if(transportOrderInfo[transOrderId].delivered == 1){
transportOrderInfo[transOrderId].statusOrder = 2;
storeOrderInfo[orderId].statusOrder = 2;
}
}
else if(orderType == 2){
transportOrderInfo[orderId].delivered = newStatus;
uint orderT = transportOrderInfo[orderId].orderType;
uint orderI = transportOrderInfo[orderId].orderTaken;
if(orderT == 0){
s = buyerOrderInfo[orderI].buyerId;
r = cropInfo[buyerOrderInfo[orderI].cropId].ownId;
m = cropInfo[buyerOrderInfo[orderI].cropId].price;
userInfo[s].balance -= m;
userInfo[r].balance += m;
if(buyerOrderInfo[orderI].delivered == 1){
transportOrderInfo[orderId].statusOrder = 2;
buyerOrderInfo[orderI].statusOrder = 2;
}
}
else if(orderT == 1){
r = storeOrderInfo[orderI].storeId;
s = cropInfo[storeOrderInfo[orderI].cropId].ownId;
m = storeInfo[storeOrderInfo[orderI].storeId].price;
userInfo[s].balance -= m;
userInfo[r].balance += m;
if(storeOrderInfo[orderI].delivered == 1){
transportOrderInfo[orderId].statusOrder = 2;
storeOrderInfo[orderI].statusOrder = 2;
}
}
}
}
function changePicked(uint orderType, uint orderId, uint newStatus) returns (bool) { ////orderType: 0--buyer 1--store 2--transport
uint transOrderId;
if(orderType == 0){
buyerOrderInfo[orderId].picked = newStatus;
transOrderId = buyerOrderInfo[orderId].transportOrderId;
if(transportOrderInfo[transOrderId].picked == 1){
transportOrderInfo[transOrderId].statusOrder = 1;
buyerOrderInfo[orderId].statusOrder = 1;
}
}
else if(orderType == 1){
storeOrderInfo[orderId].picked = newStatus;
transOrderId = storeOrderInfo[orderId].transportOrderId;
if(transportOrderInfo[transOrderId].picked == 1){
transportOrderInfo[transOrderId].statusOrder = 1;
storeOrderInfo[orderId].statusOrder = 1;
}
}
else if(orderType == 2){
transportOrderInfo[orderId].picked = newStatus;
uint orderT = transportOrderInfo[orderId].orderType;
uint orderI = transportOrderInfo[orderId].orderTaken;
if(orderT == 0){
if(buyerOrderInfo[orderI].picked == 1){
transportOrderInfo[orderId].statusOrder = 1;
buyerOrderInfo[orderI].statusOrder = 1;
}
}
else if(orderT == 1){
if(storeOrderInfo[orderI].picked == 1){
transportOrderInfo[orderId].statusOrder = 1;
storeOrderInfo[orderI].statusOrder = 1;
}
}
}
}
function changeAccept(uint orderType, uint orderId, uint newStatus) returns (bool) { ////orderType: 0--buyer 1--store 2--transport
if(orderType == 0){
buyerOrderInfo[orderId].accept = newStatus;
buyerOrderInfo[orderId].storeId = 0;
}
else if(orderType == 1){
storeOrderInfo[orderId].accept = newStatus;
}
else if(orderType == 2){
transportOrderInfo[orderId].accept = newStatus;
}
}
}