-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.sol
206 lines (206 loc) · 5.17 KB
/
contract.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
pragma solidity ^0.8.14;
contract drugcompany
{
struct product
{
uint avail;
uint threosold;
uint rate;
}
mapping (uint => product)drugs;
address producer;
address drugmanager;
address hospitalmanager;
// uint public prodid=0;
uint orderdid=0;
struct order
{
uint hospid;
uint time;
uint drugid;
uint quantity;
uint amount;
}
mapping (uint => order)orders;
uint [] hospitals;
uint public income=0;
struct patient
{
uint hospid;
}
struct patient_log
{
uint patientid;
uint docid;
uint timestamp;
uint drugid;
uint amount;
}
event produced_drug_event(uint pid,uint a);
event added_drug_event(uint p,uint a,uint t,uint r);
event drug_bought_event(uint h,uint porid,uint req_amount,uint did,uint patid);
uint [] patientarr;
uint [] doctors;
mapping (uint => patient) patients;
mapping (uint => patient_log) logs;
uint logid=1;
modifier onlydm(address d)
{
require(d==drugmanager,"only drug manager is allowed");
_;
}
modifier onlyhm(address d)
{
require(d==hospitalmanager,"only hospital manager is allowed");
_;
}
function reg_producer(address w) public onlydm(msg.sender)
{
producer=w;
}
constructor(address hm)
{
drugmanager=msg.sender;
hospitalmanager=hm;
}
struct producer_content
{
uint amount;
}
mapping (uint => producer_content) produced_drug;
modifier onlyproducer(address j)
{
require(j==producer,"only producer is allowed");
_;
}
function add_produced_drug(uint pid,uint a) public onlyproducer(msg.sender)
{
produced_drug[pid].amount=a;
emit produced_drug_event(pid,a);
}
function reg_hos(uint y) public onlyhm(msg.sender)
{
hospitals.push(y);
}
function reg_patient(uint y,uint hi) public onlyhm(msg.sender)
{
patientarr.push(y);
patients[y].hospid=hi;
}
function reg_doctor(uint y) public onlyhm(msg.sender)
{
doctors.push(y);
}
modifier validam(uint d,uint p)
{
require(produced_drug[p].amount==d,"not correct data is entered");
_;
}
function add_drug(uint p,uint a,uint t,uint r) public onlydm(msg.sender) validam(a,p)
{
drugs[p].avail=a;
drugs[p].threosold=t;
drugs[p].rate=r;
// prodid+=1;
emit added_drug_event(p,a,t,r);
}
function update_avail(uint pi,uint k) public onlydm(msg.sender) validam(k,pi)
{
drugs[pi].avail+=k;
}
function valid_hos(uint j) private view returns(bool)
{
uint flag=0;
for(uint i=0;i<hospitals.length;i++)
{
if(hospitals[i]==j)
{
flag=1;
return true;
}
}
if (flag==0)
{
return false;
}
}
function valid_pait(uint j) private view returns(bool)
{
uint flag=0;
for(uint i=0;i<patientarr.length;i++)
{
if(patientarr[i]==j)
{
flag=1;
return true;
}
}
if (flag==0)
{
return false;
}
}
function valid_doctors(uint j) private view returns(bool)
{
uint flag=0;
for(uint i=0;i<doctors.length;i++)
{
if(doctors[i]==j)
{
flag=1;
return true;
}
}
if (flag==0)
{
return false;
}
}
modifier validhos(uint tr)
{
require(valid_hos(tr)==true,"it is not a valid hospital");
_;
}
modifier validp(uint tr)
{
require(valid_pait(tr)==true,"it is not a valid paitent");
_;
}
modifier validd(uint tr)
{
require(valid_doctors(tr)==true,"it is not a valid doctor");
_;
}
modifier meetavail(uint hy,uint k)
{
require(hy<= drugs[k].avail,"requested amount of drug is not available");
_;
}
modifier meetthros(uint k,uint hy)
{
require(hy< drugs[k].threosold,"requested amount is greater than the threesold");
_;
}
function buy_drug (uint h,uint porid,uint req_amount,uint did,uint patid) public validhos(h) meetavail(req_amount,porid) meetthros(porid,req_amount) validp(patid) validd(did)
{
drugs[porid].avail-=req_amount;
income+=(req_amount*drugs[porid].rate);
orders[orderdid].hospid=h;
orders[orderdid].time=block.timestamp;
orders[orderdid].drugid=porid;
orders[orderdid].quantity=req_amount;
orders[orderdid].amount=req_amount*drugs[porid].rate;
orderdid+=1;
logs[logid].patientid=patid;
logs[logid].docid=did;
logs[logid].timestamp=block.timestamp;
logs[logid].drugid=porid;
logs[logid].amount=req_amount;
logid+=1;
emit drug_bought_event(h,porid,req_amount,did,patid);
}
function drug_av(uint pid) view public returns(uint)
{
return drugs[pid].avail;
}
}