forked from dineshpabbi10/SupplyChain-Blockchain-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SupplyChain.sol
384 lines (283 loc) · 13.7 KB
/
SupplyChain.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
pragma solidity >=0.5.0 ;
pragma experimental ABIEncoderV2
contract SupplyChain
{
string public company_name;
address payable companyAddress;
uint distributor_profit2=1 ether;
struct Order{
string name;
uint quantity;
string status;
uint orderId;
uint orderPayment;
}
struct component{
address owner;
uint price;
string componentName;
string manufacturer;
}
struct product{
address owner;
uint price;
bool isSold;
string name;
uint productId;
bool isCompleted; // TO BE MADE INTO ENUM
}
struct partners
{
string Partnername;
}
struct inventory
{
uint productCount;
uint cotton;
uint shirt;
uint pants;
uint tshirt;
}
mapping(address=>mapping(string =>component)) fetchIndividualComponent;
mapping(address => partners) companies;
mapping(string => component) manuComponent;
mapping(address=>Order[]) partnerOrders;
mapping(address=>mapping(string=>inventory)) companyInventory;
uint public myEtherValue=1 ether; // to convert any given value to ether
event productCreated(
address owner,
uint price,
bool isSold,
string name,
uint productId,
bool isCompleted,
uint productCount
);
mapping (address => product[]) products;
mapping( string => component[]) components;
mapping(string=>string[]) productToComponentMapping;
uint retailer_profit2=1 ether;
event orderCreated
(
string name,
uint quantity,
string status,
uint orderId
);
event orderStatus
(
string name,
uint quantity,
string status,
uint orderId
);
event orderCreatedforCompany(
string name,
uint quantity,
string status,
uint orderId,
uint orderPayment
);
event orderCreatedfordistributor(
string name,
uint quantity,
string status,
uint orderId,
uint orderPayment
);
function giveOrder(string memory _Componentname, uint number, address _partnerCompany)public payable
{
Order memory order1;
order1.status="pending";
order1.quantity=number;
order1.name=_Componentname;
component memory component1=fetchIndividualComponent[_partnerCompany][order1.name];
component1.price=1 ether;// change price and make contsnats on top
component1.componentName = order1.name;
component1.owner = _partnerCompany;
component1.manufacturer = companies[_partnerCompany].Partnername;
fetchIndividualComponent[component1.owner][component1.componentName]=component1;
manuComponent[_Componentname]=component1;
uint totalPrice = component1.price*order1.quantity;
require(msg.value==totalPrice);
order1.orderPayment=msg.value;
Order[] memory orders= partnerOrders[_partnerCompany];
order1.orderId=orders.length+1;
partnerOrders[_partnerCompany].push(order1);
// to let company know of his Id of
emit orderCreated(order1.name, order1.quantity,order1.status,order1.orderId);
}
function orderInProgress(uint _orderId) public
{
Order memory order1= partnerOrders[msg.sender][_orderId];
order1.status="inProgress";
emit orderStatus(order1.name,order1.quantity,order1.status,order1.orderId);
}
function signUp(string memory companyName) public{
companies[msg.sender] = partners(companyName);
}
function orderCompleted(uint _orderId) public payable // change according to manufacturer
{
Order memory order1=partnerOrders[msg.sender][_orderId];
order1.status="completed";
string memory _Componentname = order1.name; // to set the inventory of component we have fetched component name from
inventory memory inventory1= companyInventory[companyAddress][_Componentname];
inventory1.productCount=inventory1.productCount+ order1.quantity;
companyInventory[companyAddress][_Componentname] = inventory1;
component memory component1=fetchIndividualComponent[msg.sender][order1.name];
component1.owner = companyAddress;
fetchIndividualComponent[companyAddress][component1.componentName]=component1;
msg.sender.transfer(order1.orderPayment); // transfer payment to the manufacturer
//component1.owner=companyAddress;
}
function createProduct(string memory _productName, uint _makingPrice) public // product created by the company
{
inventory memory inventory1= companyInventory[msg.sender][_productName]; // fetching initial inventory
require(msg.sender==companyAddress);
uint price=0;
string[] memory ingredients = productToComponentMapping[_productName];
for (uint i = 0; i < ingredients.length; i++)
{
inventory memory ingredientInventory = companyInventory[msg.sender][ingredients[i]];
require(ingredientInventory.productCount>0);
ingredientInventory.productCount = ingredientInventory.productCount-1;
components[_productName].push(manuComponent[ingredients[i]]);
companyInventory[msg.sender][ingredients[i]]=ingredientInventory;
price = price+fetchIndividualComponent[msg.sender][ingredients[i]].price;
}
product memory product1;
product1.name=_productName;
product1.owner=msg.sender;
_makingPrice=_makingPrice*myEtherValue;
product1.price=price+_makingPrice;
product1.isSold=false;
product[] memory productsList = products[msg.sender]; // for adding product to the product list.
product1.productId=productsList.length+1;
product1.isCompleted=true;
inventory1.productCount=inventory1.productCount+1; // increasing inventory
products[msg.sender].push(product1);
companyInventory[msg.sender][_productName]=inventory1;
emit productCreated(product1.owner,product1.price,product1.isSold,product1.name, product1.productId,product1.isCompleted,inventory1.productCount);
}
// This function is used to set menu for product components' name in a product
function setProductComponents() public returns(string memory){
productToComponentMapping["DesignedShirt"].push("rawShirt");
productToComponentMapping["DesignedShirt"].push("thread");
return "components have been set";
}
// The distributor sends his requirement to the company, mentioning the productname and quantity required
function distributorRequirement(string memory _productName, uint quantity) public payable{
product[] memory productList= products[companyAddress];
Order memory order1;
order1.status="pending";
order1.quantity=quantity;
order1.name=_productName;
uint totalPrice = 0;
for (uint i = 0; i < productList.length; i++) {
product memory product1=products[companyAddress][i];
string memory productOrderedName = product1.name;
if( (keccak256(abi.encodePacked((productOrderedName))) == keccak256(abi.encodePacked((_productName))) ))
{
totalPrice=products[companyAddress][i].price*quantity;
}
}
require(msg.value==totalPrice);
order1.orderPayment=msg.value;
partnerOrders[companyAddress].push(order1);
emit orderCreatedforCompany(order1.name, order1.quantity,order1.status,order1.orderId,order1.orderPayment);
}
// The Company checks stock available for the distributor's order and sends the products to the distributor and pays the transportation fees
function checkStock(uint _orderId, address _distributor,address payable _transporter ) public payable{ // change according to new manufacturer 2 layer
Order memory order1=partnerOrders[msg.sender][_orderId];
order1.status="completed";
string memory _productOrderedName = order1.name; // to set the inventory of component we have fetched component name from
inventory memory inventory1= companyInventory[companyAddress][order1.name];
inventory1.productCount=inventory1.productCount- order1.quantity;
companyInventory[companyAddress][_productOrderedName] = inventory1;
partnerOrders[msg.sender][_orderId]=order1;
msg.sender.transfer(order1.orderPayment);
inventory memory inventory2= companyInventory[_distributor][order1.name];
inventory2.productCount=inventory2.productCount+1;
companyInventory[_distributor][order1.name]=inventory2;
uint transportation_cost=order1.orderPayment/100; // he should know what transportation he has to pay
product memory product2= products[companyAddress][_orderId];
product2.owner=_distributor;
product2.price=product2.price+distributor_profit2;
products[_distributor].push(product2);
transportStocktoDistributor(transportation_cost,_transporter,_distributor, order1.name ) ;
}
//This function is used by the company which is automatically called to pay the transporter account
function transportStocktoDistributor(uint _transportationcost, address payable transporteraddress, address _distributor, string memory _name) public payable
{
transporteraddress.transfer(_transportationcost);
}
function retailerRequirement(string memory _productName, uint quantity, address _distributor) public payable
{
product[] memory productList= products[_distributor];
Order memory order1;
order1.status="pending";
order1.quantity=quantity;
order1.name=_productName;
uint totalPricetoRetailer = 0;
for (uint i = 0; i < productList.length; i++) {
product memory product1=products[_distributor][i];
string memory productOrderedName = product1.name;
if( (keccak256(abi.encodePacked((productOrderedName))) == keccak256(abi.encodePacked((_productName))) ))
{
totalPricetoRetailer=products[_distributor][i].price*quantity;
}
}
require(msg.value==totalPricetoRetailer);
order1.orderPayment=msg.value;
partnerOrders[_distributor].push(order1);
emit orderCreatedfordistributor(order1.name, order1.quantity,order1.status,order1.orderId,order1.orderPayment);
}
function checkdistributorStock(uint _orderId, address _retailer,address payable _transporter ) public payable // change according to new manufacturer 2 layer
{
Order memory order1=partnerOrders[msg.sender][_orderId];
order1.status="completed";
string memory _productOrderedName = order1.name; // to set the inventory of component we have fetched component name from
inventory memory inventory1= companyInventory[msg.sender][order1.name];
inventory1.productCount=inventory1.productCount- order1.quantity;
companyInventory[msg.sender][_productOrderedName] = inventory1;
partnerOrders[msg.sender][_orderId]=order1;
msg.sender.transfer(order1.orderPayment);
inventory memory inventory2= companyInventory[_retailer][order1.name];
inventory2.productCount=inventory2.productCount+order1.quantity;
companyInventory[_retailer][order1.name]=inventory2;
uint transportation_cost=order1.orderPayment/100; // he should know what transportation he has to pay
product memory product2= products[msg.sender][_orderId];
product2.owner=_retailer;
product2.price=product2.price+retailer_profit2;
products[_retailer].push(product2);
transportStocktoRetailer(transportation_cost,_transporter,_retailer, order1.name);
}
function transportStocktoRetailer(uint _transportationcost, address payable transporteraddress, address _retailer, string memory _name) public payable
{
transporteraddress.transfer(_transportationcost);
}
// function for customer to buy products
function sellProductToCustomer(address payable _retailer,string memory _productName) public payable
{
product[] memory productList= products[_retailer];
Order memory order1;
order1.status="completed";
// order1.quantity=quantity;
// order1.name=_productName;
partnerOrders[_retailer].push(order1);
//uint totalPricetoRetailer = 0;
for (uint i = 0; i < productList.length; i++) {
// `Proposal({...})` creates a temporary
// Proposal object and `proposals.push(...)`
// appends it to the end of `proposals`.
product memory product1=products[_retailer][i];
string memory productOrderedName = product1.name;
if( (keccak256(abi.encodePacked((productOrderedName))) == keccak256(abi.encodePacked((_productName))) ))
{
//totalPricetoRetailer=products[_retailer][i].price*quantity;
_retailer.transfer(msg.value);
product1.owner=msg.sender;
}
}
}
}