-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contract.sol
201 lines (201 loc) · 4.57 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
Solidiy File :
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;
}
uint [] patientarr;
uint [] doctors;
mapping (uint => patient) patients;
mapping (uint => patient_log) logs;
uint logid=1;
modifier onlydm(address d)
{
require(d==drugmanager);
_;
}
modifier onlyhm(address d)
{
require(d==hospitalmanager);
_;
}
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);
_;
}
function add_produced_drug(uint pid,uint a) public onlyproducer(msg.sender)
{
produced_drug[pid].amount=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);
_;
}
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;
}
function update_avail(uint pi,uint k) public onlydm(msg.sender) validam(k,pi)
{
drugs[pi].avail+=k;
}
function valid_hos(uint j) public 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) public 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) public 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);
_;
}
modifier validp(uint tr)
{
require(valid_pait(tr)==true);
_;
}
modifier validd(uint tr)
{
require(valid_doctors(tr)==true);
_;
}
modifier meetavail(uint hy,uint k)
{
require(hy<= drugs[k].avail);
_;
}
modifier meetthros(uint k,uint hy)
{
require(hy< drugs[k].threosold);
_;
}
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) onlyhm(msg.sender)
{
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;
}
function drug_av(uint pid) view public returns(uint)
{
return drugs[pid].avail;
}
}