Skip to content

Commit

Permalink
Migrate remaining usage of Closing
Browse files Browse the repository at this point in the history
  • Loading branch information
antweb committed Nov 1, 2024
1 parent 9525997 commit 87434df
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Closing> closings = dataStore.select(Closing.class)
.where(Closing.OPEN.eq(false))
.and(Closing.SERVER_ID.isNull())
.get().toList();
List<Closing> closings = db.getClosingQueries().selectClosedWithoutServerId().executeAsList();

try {
int i = 0;
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -40,3 +47,9 @@ INSERT INTO Closing (
?,
?
);

updateServerId:
UPDATE Closing
SET
server_id = ?
WHERE id = ?;

0 comments on commit 87434df

Please sign in to comment.