This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeliveryWithLocations.sol
146 lines (100 loc) · 3.95 KB
/
DeliveryWithLocations.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
pragma solidity ^0.5.0;
contract DeliveryWithLocations {
/// The seller's address
address public owner;
/// The buyer's address part on this contract
address public buyerAddr;
struct Location {
string currentLocation;
uint timestamp;
}
/// The Order struct
struct Order {
string goods;
uint quantity;
uint price;
uint discountIfLate; // if delivery late or bad location data price discounted by this much out of the price
uint timestamp;
uint locationFrequency;
string deliveryLocation;
uint deliveryTime;
bool accepted;
}
/// The Invoice struct
struct Invoice {
uint orderno;
uint number;
bool init;
}
/// Single Order per contract
Order public order;
Location[] public locations;
/// Event triggered when order is accepted
event OrderAccepted(address buyer, string goods, uint quantity, string deliveryLocation);
/// Event triggerd when the location of shipment is updated
event LocationUpdated(string location);
/// Event triggered when too slow to deliver and buyer doesnt wait
event Refunded();
/// Event triggered when the courie delives the order
event OrderDelivered(address buyer, string goods, uint quantity, uint real_delivey_date, address courier);
/// The smart contract's constructor
constructor (address _buyerAddr, string memory goods, uint quantity, uint price, uint discountIfLate, uint locationFrequency, string memory deliveryLocation, uint deliveryTime) public payable {
/// The seller is the contract's owner
owner = msg.sender;
order = Order(goods, quantity, price, discountIfLate, now, locationFrequency, deliveryLocation, deliveryTime, false);
buyerAddr = _buyerAddr;
}
function agreeToTerms() payable public {
/// Accept orders just from buyer
require(msg.sender == buyerAddr);
require(!order.accepted);
require(order.price == msg.value);
order.accepted = true;
order.timestamp = now;
/// Trigger the event
emit OrderAccepted(msg.sender, order.goods, order.quantity, order.deliveryLocation);
}
function getLocationCount() public view returns(uint entityCount) {
return locations.length;
}
/// The function update location data
function sendLocationUpdate(string memory CurrentLocation) public {
/// Validate order accepted
require(order.accepted);
/// Just the seller can send the invoice
require(owner == msg.sender);
Location memory newLocation;
newLocation.currentLocation = CurrentLocation;
newLocation.timestamp = now;
locations.push(newLocation)-1;
emit LocationUpdated(CurrentLocation);
}
function RefundUndelivered() payable public {
/// Validate order accepted
require(order.accepted);
require(order.timestamp + order.deliveryTime < now);///late
/// Just the seller can send the invoice
require(buyerAddr == msg.sender);
msg.sender.transfer(order.price);
emit Refunded();
}
/// The function to mark an order as delivered
function delivered() payable public {
/// Validate the invoice number
require(order.accepted);
/// Just the shipper can call this function
require(owner == msg.sender);
if(locations.length >= order.locationFrequency && order.timestamp + order.deliveryTime < now ){
/// Payout the Order to the seller on time
msg.sender.transfer(order.price);
}
else{/// Payout the Order to the seller late or bad locations
msg.sender.transfer(order.price - order.discountIfLate );
address(uint160(buyerAddr)).transfer(order.price - (order.price - order.discountIfLate));
}
emit OrderDelivered(buyerAddr, order.goods, order.quantity, now, owner);
}
function health() pure public returns (string memory) {
return "running";
}
}