-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinvoice.sol
88 lines (68 loc) · 1.91 KB
/
invoice.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract InvoiceContract is ReentrancyGuard {
using Counters for Counters.Counter;
Counters.Counter private _itemIds;
address payable owner;
constructor() {
owner = payable(msg.sender);
}
struct invoice {
uint256 id;
address from;
address to;
uint256 createDate;
uint256 dueDate;
}
mapping(uint256 => invoice) private invoices;
event InvoiceCreated(
uint256 indexed id,
address indexed from,
address indexed to,
uint256 createDate,
uint256 dueDate
);
function createInvoice(
address _from,
address _to,
uint256 _createDate,
uint256 _dueDate
) public payable nonReentrant {
_itemIds.increment();
uint256 invoiceId = _itemIds.current();
invoices[invoiceId] = invoice(
invoiceId,
_from,
_to,
_createDate,
_dueDate
);
emit InvoiceCreated(invoiceId, _from, _to, _createDate, _dueDate);
}
function getInvoice(uint256 invoiceId)
public
view
returns (invoice memory)
{
return invoices[invoiceId];
}
function getInvoices(
)
public
view
returns (invoice[] memory)
{
uint256 itemCount = _itemIds.current();
uint256 currentIndex = 0;
invoice[] memory items = new invoice[](itemCount);
for (uint256 i = 0; i < itemCount; i++) {
uint256 currentId = invoices[i + 1].id;
invoice storage currentInvoice = invoices[currentId];
items[currentIndex] = currentInvoice;
currentIndex += 1;
}
return items;
}
}