-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInvoice.java
More file actions
252 lines (221 loc) · 12.4 KB
/
Invoice.java
File metadata and controls
252 lines (221 loc) · 12.4 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package pro.cloudnode.smp.bankaccounts;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Level;
import java.util.stream.Collectors;
/**
* A payment request
*/
public class Invoice {
public final @NotNull String id;
/**
* The beneficiary
*/
public final @NotNull Account seller;
public final @NotNull BigDecimal amount;
private final @Nullable String description;
/**
* A potential buyer/payer. If set, only they can see and pay the invoice. If `null`, the invoice is public.
*/
private final @Nullable OfflinePlayer buyer;
public final @NotNull Date created;
/**
* Payment transaction. If set, the invoice is paid.
*/
public @Nullable Transaction transaction;
public Invoice(final @NotNull String id, final @NotNull Account seller, final @NotNull BigDecimal amount, final @Nullable String description, final @Nullable OfflinePlayer buyer, final @NotNull Date created, final @Nullable Transaction transaction) {
this.id = id;
this.seller = seller;
this.amount = amount;
this.description = description;
this.buyer = buyer;
this.created = created;
this.transaction = transaction;
}
public Invoice(final @NotNull Account seller, final @NotNull BigDecimal amount, final @Nullable String description, final @Nullable OfflinePlayer buyer) {
this(StringGenerator.generate(16), seller, amount, description, buyer, new Date(), null);
}
public Invoice(final @NotNull ResultSet rs) throws @NotNull SQLException {
this(
rs.getString("id"),
Account.get(Account.Tag.id(rs.getString("seller"))).orElse(new Account.ClosedAccount()),
rs.getBigDecimal("amount"),
rs.getString("description"),
rs.getString("buyer") == null ? null : BankAccounts.getInstance().getServer().getOfflinePlayer(UUID.fromString(rs.getString("buyer"))),
new Date(rs.getTimestamp("created").getTime()),
Transaction.get(rs.getInt("transaction")).orElse(null)
);
}
public @NotNull Optional<@NotNull String> description() {
return Optional.ofNullable(description);
}
public @NotNull Optional<@NotNull OfflinePlayer> buyer() {
return Optional.ofNullable(buyer);
}
public void pay(final @NotNull Account buyer) {
transaction = buyer.transfer(seller, amount, "Invoice #" + id, null);
update();
}
public void insert() {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("INSERT INTO `bank_invoices` (`id`, `seller`, `amount`, `description`, `buyer`, `created`, `transaction`) VALUES (?, ?, ?, ?, ?, ?, ?)")) {
stmt.setString(1, id);
stmt.setString(2, seller.id);
stmt.setBigDecimal(3, amount);
if (description == null) stmt.setNull(4, java.sql.Types.VARCHAR);
else stmt.setString(4, description);
if (buyer == null) stmt.setNull(5, java.sql.Types.VARCHAR);
else stmt.setString(5, buyer.getUniqueId().toString());
stmt.setTimestamp(6, new java.sql.Timestamp(created.getTime()));
if (transaction == null) stmt.setNull(7, java.sql.Types.INTEGER);
else stmt.setInt(7, transaction.getId());
stmt.executeUpdate();
} catch (final @NotNull SQLException e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not save invoice: " + id, e);
}
}
public void update() {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("UPDATE `bank_invoices` SET `transaction` = ? WHERE `id` = ?")) {
if (transaction == null) stmt.setNull(1, Types.INTEGER);
else stmt.setInt(1, transaction.getId());
stmt.setString(2, id);
stmt.executeUpdate();
} catch (final @NotNull SQLException e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not update invoice: " + id, e);
}
}
public static @NotNull Optional<@NotNull Invoice> get(final @NotNull String id) {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `bank_invoices` WHERE `id` = ? LIMIT 1")) {
stmt.setString(1, id);
final @NotNull ResultSet rs = stmt.executeQuery();
if (rs.next()) return Optional.of(new Invoice(rs));
return Optional.empty();
}
catch (final @NotNull SQLException e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get invoice: " + id, e);
return Optional.empty();
}
}
public static @NotNull Invoice @NotNull [] get(final @NotNull OfflinePlayer player) {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `bank_invoices` where `buyer` = ?")) {
stmt.setString(1, player.getUniqueId().toString());
final @NotNull ResultSet rs = stmt.executeQuery();
final @NotNull List<@NotNull Invoice> invoices = new ArrayList<>();
while (rs.next()) invoices.add(new Invoice(rs));
return invoices.toArray(new @NotNull Invoice[0]);
}
catch (final @NotNull SQLException e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get invoices for player: " + player.getUniqueId(), e);
return new @NotNull Invoice[0];
}
}
public static @NotNull Invoice @NotNull [] get(final @NotNull OfflinePlayer player, final int limit, final int offset) {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `bank_invoices` where `buyer` = ? ORDER BY `created` DESC LIMIT ? OFFSET ?")) {
stmt.setString(1, player.getUniqueId().toString());
stmt.setInt(2, limit);
stmt.setInt(3, offset);
final @NotNull ResultSet rs = stmt.executeQuery();
final @NotNull List<@NotNull Invoice> invoices = new ArrayList<>();
while (rs.next()) invoices.add(new Invoice(rs));
return invoices.toArray(new @NotNull Invoice[0]);
}
catch (final @NotNull SQLException e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get invoices for player: " + player.getUniqueId(), e);
return new @NotNull Invoice[0];
}
}
public static @NotNull Invoice @NotNull [] get(final @NotNull OfflinePlayer player, final @NotNull Account @NotNull [] seller) {
final @NotNull String inParams = Arrays.stream(seller).map(s -> "?").collect(Collectors.joining(", "));
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `bank_invoices` where `buyer` = ? OR `seller` IN (" + inParams + ")")) {
stmt.setString(1, player.getUniqueId().toString());
for (int i = 0; i < seller.length; ++i) stmt.setString(i + 2, seller[i].id);
final @NotNull ResultSet rs = stmt.executeQuery();
final @NotNull List<@NotNull Invoice> invoices = new ArrayList<>();
while (rs.next()) invoices.add(new Invoice(rs));
return invoices.toArray(new Invoice[0]);
}
catch (final @NotNull SQLException e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get invoices for buyer & seller: " + player.getUniqueId(), e);
return new @NotNull Invoice[0];
}
}
public static @NotNull Invoice @NotNull [] get(final @NotNull OfflinePlayer player, final @NotNull Account @NotNull [] seller, final int limit, final int offset) {
final @NotNull String inParams = Arrays.stream(seller).map(s -> "?").collect(Collectors.joining(", "));
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `bank_invoices` where `buyer` = ? OR `seller` IN (" + inParams + ") ORDER BY `created` DESC LIMIT ? OFFSET ?")) {
stmt.setString(1, player.getUniqueId().toString());
for (int i = 0; i < seller.length; ++i) stmt.setString(i + 2, seller[i].id);
stmt.setInt(seller.length + 2, limit);
stmt.setInt(seller.length + 3, offset);
final @NotNull ResultSet rs = stmt.executeQuery();
final @NotNull List<@NotNull Invoice> invoices = new ArrayList<>();
while (rs.next()) invoices.add(new Invoice(rs));
return invoices.toArray(new Invoice[0]);
}
catch (final @NotNull SQLException e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get invoices for buyer & seller: " + player.getUniqueId(), e);
return new @NotNull Invoice[0];
}
}
public static @NotNull Invoice @NotNull [] get(final @NotNull Account @NotNull [] seller, final int limit, final int offset) {
final @NotNull String inParams = Arrays.stream(seller).map(s -> "?").collect(Collectors.joining(", "));
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `bank_invoices` where `seller` IN (" + inParams + ") ORDER BY `created` DESC LIMIT ? OFFSET ?")) {
for (int i = 0; i < seller.length; ++i) stmt.setString(i + 1, seller[i].id);
stmt.setInt(seller.length + 1, limit);
stmt.setInt(seller.length + 2, offset);
final @NotNull ResultSet rs = stmt.executeQuery();
final @NotNull List<@NotNull Invoice> invoices = new ArrayList<>();
while (rs.next()) invoices.add(new Invoice(rs));
return invoices.toArray(new Invoice[0]);
}
catch (final @NotNull SQLException e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get invoices for seller: " + Arrays.toString(seller), e);
return new @NotNull Invoice[0];
}
}
public static @NotNull Invoice @NotNull [] get() {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `bank_invoices`")) {
final @NotNull ResultSet rs = stmt.executeQuery();
final @NotNull List<@NotNull Invoice> invoices = new ArrayList<>();
while (rs.next()) invoices.add(new Invoice(rs));
return invoices.toArray(new @NotNull Invoice[0]);
}
catch (final @NotNull SQLException e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get invoices", e);
return new @NotNull Invoice[0];
}
}
public static int countUnpaid(final @NotNull OfflinePlayer player) {
try (final @NotNull Connection conn = BankAccounts.getInstance().getDb().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT COUNT(`id`) as `count` FROM `bank_invoices` WHERE `buyer` = ? AND `transaction` IS NULL")) {
stmt.setString(1, player.getUniqueId().toString());
final @NotNull ResultSet rs = stmt.executeQuery();
if (rs.next()) return rs.getInt("count");
return 0;
}
catch (final @NotNull SQLException e) {
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not count unpaid invoices for player: " + player.getUniqueId(), e);
return 0;
}
}
}