diff --git a/libpretixsync/src/main/java/eu/pretix/libpretixsync/sqldelight/ClosingExtensions.kt b/libpretixsync/src/main/java/eu/pretix/libpretixsync/sqldelight/ClosingExtensions.kt new file mode 100644 index 00000000..3d44b157 --- /dev/null +++ b/libpretixsync/src/main/java/eu/pretix/libpretixsync/sqldelight/ClosingExtensions.kt @@ -0,0 +1,26 @@ +package eu.pretix.libpretixsync.sqldelight + +import org.json.JSONObject +import java.math.RoundingMode +import java.text.DateFormat +import java.text.SimpleDateFormat +import java.util.TimeZone + +fun Closing.toJSON(): JSONObject { + val tz = TimeZone.getTimeZone("UTC") + val df: DateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") + df.timeZone = tz + + val jo = JSONObject() + jo.put("closing_id", id) + jo.put("first_receipt", first_receipt) + jo.put("last_receipt", last_receipt) + jo.put("payment_sum", payment_sum?.setScale(2, RoundingMode.HALF_UP)) + jo.put("payment_sum_cash", payment_sum_cash?.setScale(2, RoundingMode.HALF_UP)) + jo.put("cash_counted", cash_counted?.setScale(2, RoundingMode.HALF_UP)) + jo.put("datetime", df.format(datetime)) + jo.put("invoice_settings", invoice_settings) + jo.put("cashier", cashier_numericid) + jo.put("data", if (json_data != null) JSONObject(json_data) else JSONObject()) + return jo +} diff --git a/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java b/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java index 3315aee0..fa05a9f0 100644 --- a/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java +++ b/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java @@ -3,6 +3,8 @@ import eu.pretix.libpretixsync.api.*; import eu.pretix.libpretixsync.models.Question; import eu.pretix.libpretixsync.models.db.QuestionExtensionsKt; +import eu.pretix.libpretixsync.sqldelight.Closing; +import eu.pretix.libpretixsync.sqldelight.ClosingExtensionsKt; import eu.pretix.libpretixsync.sqldelight.QueuedCall; import eu.pretix.libpretixsync.sqldelight.QueuedCheckIn; import eu.pretix.libpretixsync.sqldelight.SyncDatabase; @@ -20,7 +22,6 @@ import eu.pretix.libpretixsync.SentryInterface; import eu.pretix.libpretixsync.config.ConfigStore; import eu.pretix.libpretixsync.db.Answer; -import eu.pretix.libpretixsync.db.Closing; import eu.pretix.libpretixsync.db.QueuedOrder; import eu.pretix.libpretixsync.db.Receipt; import eu.pretix.libpretixsync.db.ReceiptLine; @@ -672,10 +673,7 @@ protected void uploadOrders(ProgressFeedback feedback) throws SyncException { protected void uploadClosings(ProgressFeedback feedback) throws SyncException { sentry.addBreadcrumb("sync.queue", "Start closings upload"); - List closings = dataStore.select(Closing.class) - .where(Closing.OPEN.eq(false)) - .and(Closing.SERVER_ID.isNull()) - .get().toList(); + List closings = db.getClosingQueries().selectClosedWithoutServerId().executeAsList(); try { int i = 0; @@ -686,11 +684,10 @@ protected void uploadClosings(ProgressFeedback feedback) throws SyncException { i++; PretixApi.ApiResponse response = api.postResource( api.organizerResourceUrl("posdevices/" + configStore.getPosId() + "/closings"), - closing.toJSON() + ClosingExtensionsKt.toJSON(closing) ); if (response.getResponse().code() == 201) { - closing.setServer_id(response.getData().getLong("closing_id")); - dataStore.update(closing); + db.getClosingQueries().updateServerId(response.getData().getLong("closing_id"), closing.getId()); } else { throw new SyncException(response.getData().toString()); } diff --git a/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/Closing.sq b/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/Closing.sq index 1e0354c4..9fb4234b 100644 --- a/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/Closing.sq +++ b/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/Closing.sq @@ -8,6 +8,13 @@ SELECT * FROM Closing WHERE server_id = ?; +selectClosedWithoutServerId: +SELECT * +FROM Closing +WHERE + open = FALSE + AND server_id IS NULL; + insert: INSERT INTO Closing ( cash_counted, @@ -40,3 +47,9 @@ INSERT INTO Closing ( ?, ? ); + +updateServerId: +UPDATE Closing +SET + server_id = ? +WHERE id = ?;