Skip to content

Commit 1512346

Browse files
committed
Migrate remaining usage of Closing
1 parent 99cf68e commit 1512346

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package eu.pretix.libpretixsync.sqldelight
2+
3+
import org.json.JSONObject
4+
import java.math.RoundingMode
5+
import java.text.DateFormat
6+
import java.text.SimpleDateFormat
7+
import java.util.TimeZone
8+
9+
fun Closing.toJSON(): JSONObject {
10+
val tz = TimeZone.getTimeZone("UTC")
11+
val df: DateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
12+
df.timeZone = tz
13+
14+
val jo = JSONObject()
15+
jo.put("closing_id", id)
16+
jo.put("first_receipt", first_receipt)
17+
jo.put("last_receipt", last_receipt)
18+
jo.put("payment_sum", payment_sum?.setScale(2, RoundingMode.HALF_UP))
19+
jo.put("payment_sum_cash", payment_sum_cash?.setScale(2, RoundingMode.HALF_UP))
20+
jo.put("cash_counted", cash_counted?.setScale(2, RoundingMode.HALF_UP))
21+
jo.put("datetime", df.format(datetime))
22+
jo.put("invoice_settings", invoice_settings)
23+
jo.put("cashier", cashier_numericid)
24+
jo.put("data", if (json_data != null) JSONObject(json_data) else JSONObject())
25+
return jo
26+
}

libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import eu.pretix.libpretixsync.api.*;
44
import eu.pretix.libpretixsync.models.Question;
55
import eu.pretix.libpretixsync.models.db.QuestionExtensionsKt;
6+
import eu.pretix.libpretixsync.sqldelight.Closing;
7+
import eu.pretix.libpretixsync.sqldelight.ClosingExtensionsKt;
68
import eu.pretix.libpretixsync.sqldelight.QueuedCall;
79
import eu.pretix.libpretixsync.sqldelight.QueuedCheckIn;
810
import eu.pretix.libpretixsync.sqldelight.SyncDatabase;
@@ -20,7 +22,6 @@
2022
import eu.pretix.libpretixsync.SentryInterface;
2123
import eu.pretix.libpretixsync.config.ConfigStore;
2224
import eu.pretix.libpretixsync.db.Answer;
23-
import eu.pretix.libpretixsync.db.Closing;
2425
import eu.pretix.libpretixsync.db.QueuedOrder;
2526
import eu.pretix.libpretixsync.db.Receipt;
2627
import eu.pretix.libpretixsync.db.ReceiptLine;
@@ -672,10 +673,7 @@ protected void uploadOrders(ProgressFeedback feedback) throws SyncException {
672673
protected void uploadClosings(ProgressFeedback feedback) throws SyncException {
673674
sentry.addBreadcrumb("sync.queue", "Start closings upload");
674675

675-
List<Closing> closings = dataStore.select(Closing.class)
676-
.where(Closing.OPEN.eq(false))
677-
.and(Closing.SERVER_ID.isNull())
678-
.get().toList();
676+
List<Closing> closings = db.getClosingQueries().selectClosedWithoutServerId().executeAsList();
679677

680678
try {
681679
int i = 0;
@@ -686,11 +684,10 @@ protected void uploadClosings(ProgressFeedback feedback) throws SyncException {
686684
i++;
687685
PretixApi.ApiResponse response = api.postResource(
688686
api.organizerResourceUrl("posdevices/" + configStore.getPosId() + "/closings"),
689-
closing.toJSON()
687+
ClosingExtensionsKt.toJSON(closing)
690688
);
691689
if (response.getResponse().code() == 201) {
692-
closing.setServer_id(response.getData().getLong("closing_id"));
693-
dataStore.update(closing);
690+
db.getClosingQueries().updateServerId(response.getData().getLong("closing_id"), closing.getId());
694691
} else {
695692
throw new SyncException(response.getData().toString());
696693
}

libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/Closing.sq

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ SELECT *
88
FROM Closing
99
WHERE server_id = ?;
1010

11+
selectClosedWithoutServerId:
12+
SELECT *
13+
FROM Closing
14+
WHERE
15+
open = FALSE
16+
AND server_id IS NULL;
17+
1118
insert:
1219
INSERT INTO Closing (
1320
cash_counted,
@@ -40,3 +47,9 @@ INSERT INTO Closing (
4047
?,
4148
?
4249
);
50+
51+
updateServerId:
52+
UPDATE Closing
53+
SET
54+
server_id = ?
55+
WHERE id = ?;

0 commit comments

Comments
 (0)