-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSupplyChain.sol
165 lines (134 loc) · 5.41 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
pragma solidity >=0.7.0 <0.8.0;
contract MyContract{
// Producer list [address and name].
mapping(address => string) public producers;
// Products details.
int public totalProduct = 0;
struct product{
int id;
int price;
int quantity;
string product_name;
address producer_address;
}
mapping(int=>product) public products;
// Order details
int public totalOrder = 0;
struct order{
int id;
int product_id;
int quantity;
string customer_name;
string status;
string delivery_address;
address customer_address;
}
mapping(int => order) public orders;
// register producer authentication.
modifier reginsterProducerAuth() {
require(bytes(producers[msg.sender]).length == 0);
_;
}
// Add product authentication.
modifier addProductAuth() {
require(bytes(producers[msg.sender]).length > 0);
_;
}
// for all
function getTotalProduct() view public returns (int) {
return totalProduct;
}
function getTotalOrder() view public returns (int) {
return totalOrder;
}
// Seller
function isRegistered(address _addr) view public returns (bool) {
if(bytes(producers[_addr]).length == 0){
return (false);
}else{
return (true);
}
}
function reginsterProducer(string memory _name) public reginsterProducerAuth {
producers[msg.sender] = _name;
}
function addProduct(string memory _pname, int _price, int _quantity) public addProductAuth {
totalProduct += 1;
products[totalProduct] = product(totalProduct, _price, _quantity, _pname, msg.sender);
}
function getTotalProduct(address _addr) view public returns (int) {
int counter = 0;
for(int i=1; i<=totalProduct;i++){
if(products[i].producer_address == _addr){
counter++;
}
}
return (counter);
}
function getNextProduct(address _addr, int _pid) view public returns (int, int, int, string memory, bool) {
require(_pid <= totalProduct);
if(products[_pid].producer_address == _addr){
return (products[_pid].id, products[_pid].price, products[_pid].quantity, products[_pid].product_name, true);
}
return (0,0, 0, "", false);
}
function updatePrice(int _pid, int _newPrice) public {
require(products[_pid].producer_address == msg.sender);
products[_pid].price = _newPrice;
}
function getMyTotalOrder(address _addr) view public returns (int) {
int counter = 0;
for(int i=1; i<=totalOrder; i++){
if(products[orders[i].product_id].producer_address == _addr){
counter++;
}
}
return (counter);
}
function getMyNextOrderById(address _addr,int _oid) view public returns(int,int,int,string memory,string memory,string memory, bool) {
require(_oid <= totalOrder);
if(products[orders[_oid].product_id].producer_address==_addr){
return (orders[_oid].id, orders[_oid].product_id, orders[_oid].quantity, orders[_oid].customer_name, orders[_oid].status, orders[_oid].delivery_address, true);
}
return (0,0,0,"","","",false);
}
function updateOrderStatus(int _oid, string memory _status) public {
require(products[orders[_oid].product_id].producer_address == msg.sender);
if (keccak256(abi.encodePacked(_status)) == keccak256(abi.encodePacked("Rejected"))) {
products[orders[_oid].product_id].quantity+=orders[_oid].quantity;
orders[_oid].status = _status;
}else{
if(keccak256(abi.encodePacked(orders[_oid].status))!=keccak256(abi.encodePacked("Rejected"))&&
keccak256(abi.encodePacked(_status))==keccak256(abi.encodePacked("Delivered"))){
orders[_oid].status = _status;
}
}
}
// customer
function getProductById(int _pid) view public returns (int, int, int, string memory) {
require(_pid <= totalProduct);
return (products[_pid].id, products[_pid].price, products[_pid].quantity, products[_pid].product_name);
}
function placeOrder(string memory _cname, string memory _daddress, int _pid, int _quantity) public {
require(products[_pid].quantity >= _quantity);
totalOrder += 1;
orders[totalOrder] = order(totalOrder, _pid, _quantity, _cname, "Placed", _daddress, msg.sender);
products[_pid].quantity -= _quantity;
}
function getTotalOrder(address _addr) view public returns (int) {
int counter = 0;
for(int i=1; i <= totalOrder; i++){
if(_addr == orders[i].customer_address){
counter++;
}
}
return (counter);
}
function fetchNextOrderById(address _addr, int _oid) view public returns (int, int, int, string memory, string memory, string memory, bool){
require(_oid <= totalOrder);
if(_addr==orders[_oid].customer_address){
return (orders[_oid].id, orders[_oid].product_id, orders[_oid].quantity, orders[_oid].customer_name, orders[_oid].status, orders[_oid].delivery_address, true);
}
return (0, 0, 0, "", "", "", false);
}
}