Skip to content

Commit

Permalink
Tax codes (#50)
Browse files Browse the repository at this point in the history
* Add support for tax codes

* Add migration

* Add empty migration, set default TaxRule.code to null and parse taxCode on OrderPosition.toModel

---------

Co-authored-by: Maximilian Richt <[email protected]>
  • Loading branch information
raphaelm and robbi5 authored Dec 13, 2024
1 parent 4961670 commit 7e9a96e
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ public BigDecimal getPrice() {
}
}

public String getTaxCode() {
try {
return getJSON().optString("tax_code", null);
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}

public BigDecimal getTaxRate() {
try {
return new BigDecimal(getJSON().getString("tax_rate"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class AbstractReceiptLine implements LocalObject {

public Long tax_rule;

public String tax_code;

public BigDecimal tax_value;

@Nullable
Expand Down Expand Up @@ -196,6 +198,7 @@ public JSONObject toJSON() throws JSONException {
jo.put("tax_rate", tax_rate != null ? tax_rate.setScale(2, RoundingMode.HALF_UP) : "0.00");
jo.put("tax_value", tax_value != null ? tax_value.setScale(2, RoundingMode.HALF_UP) : "0.00");
jo.put("tax_rule", tax_rule != null ? tax_rule : JSONObject.NULL);
jo.put("tax_code", tax_code != null ? tax_code : JSONObject.NULL);
jo.put("secret", secret);
jo.put("seat", seat_guid != null ? seat_guid : JSONObject.NULL);
jo.put("subevent", subevent_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ public boolean includesTax() {
}
}

public String getCode() {
try {
if (!getJSON().has("code") || getJSON().isNull("code")) {
return null;
}
return getJSON().getString("code");
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}

public BigDecimal getRate() {
try {
return new BigDecimal(getJSON().getString("rate"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class Migrations {
private static EntityModel model = Models.DEFAULT;
public static int CURRENT_VERSION = 105;
public static int CURRENT_VERSION = 106;

private static void createVersionTable(Connection c, int version) throws SQLException {
Statement s2 = c.createStatement();
Expand Down Expand Up @@ -430,6 +430,10 @@ public static void migrate(DataSource dataSource, boolean dbIsNew) throws SQLExc
execIgnore(c, "ALTER TABLE settings DROP COLUMN covid_certificates_allow_vaccinated_products;", new String[] {"no such column", "existiert", "syntax error"});
updateVersionTable(c, 105);
}
if (db_version < 106) {
execIgnore(c, "ALTER TABLE receiptline ADD tax_code TEXT NULL;", new String[] {"duplicate column name", "already exists", "existiert bereits"});
updateVersionTable(c, 106);
}

// Note that the Android app currently does not use these queries!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class OrderPosition(
val price: BigDecimal? = null,
val taxRate: BigDecimal? = null,
val taxValue: BigDecimal? = null,
val taxCode: String? = null,
val seatName: String? = null,
val addonToServerId: Long? = null,
val blocked: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ data class ReceiptLine(
val taxRate: BigDecimal? = null,
val taxRule: Long? = null,
val taxValue: BigDecimal? = null,
val taxCode: String? = null,
val eventDateFrom: OffsetDateTime? = null,
val eventDateTo: OffsetDateTime? = null,
val subEventServerId: Long? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ data class TaxRule(
val serverId: Long,
val rate: BigDecimal = BigDecimal("0.00"),
val includesTax: Boolean = false,
val code: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fun OrderPosition.toModel(): OrderPositionModel {
zipcode = json.optString("zipcode", null),
price = parsePrice(json),
taxRate = parseTaxRate(json),
taxCode = parseTaxCode(json),
taxValue = parseTaxValue(json),
seatName = parseSeatName(json),
addonToServerId = parseAddonToServerId(json),
Expand All @@ -50,6 +51,15 @@ private fun parsePrice(json: JSONObject): BigDecimal? {
}
}

private fun parseTaxCode(json: JSONObject): String? {
try {
return json.optString("tax_code", null)
} catch (e: JSONException) {
e.printStackTrace()
return null
}
}

private fun parseTaxRate(json: JSONObject): BigDecimal? {
try {
return BigDecimal(json.getString("tax_rate"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fun ReceiptLine.toModel() =
voucherCode = voucher_code,
useReusableMedium = use_reusable_medium,
taxRate = tax_rate,
taxCode = tax_code,
taxRule = tax_rule,
taxValue = tax_value,
eventDateFrom = SafeOffsetDateTimeMapper.decode(event_date_from),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fun TaxRule.toModel(): TaxRuleModel {
serverId = this.server_id!!,
rate = parseRate(json),
includesTax = parseIncludesTax(json),
code = parseCode(json),
)
}

Expand All @@ -34,3 +35,15 @@ private fun parseIncludesTax(json: JSONObject): Boolean {
return false
}
}

private fun parseCode(json: JSONObject): String? {
try {
if (!json.has("code") or json.isNull("code")) {
return null
}
return json.getString("code")
} catch (e: JSONException) {
e.printStackTrace()
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fun ReceiptLine.toJSON(): JSONObject {
jo.put("tax_rate", tax_rate?.setScale(2, RoundingMode.HALF_UP))
jo.put("tax_value", tax_value?.setScale(2, RoundingMode.HALF_UP) ?: "0.00")
jo.put("tax_rule", tax_rule ?: JSONObject.NULL)
jo.put("tax_code", tax_code ?: JSONObject.NULL)
jo.put("secret", secret)
jo.put("seat", seat_guid ?: JSONObject.NULL)
jo.put("subevent", subevent_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ INSERT INTO ReceiptLine (
tax_rate,
tax_rule,
tax_value,
tax_code,
type,
use_reusable_medium,
variation_id,
Expand Down Expand Up @@ -102,5 +103,6 @@ VALUES (
?,
?,
?,
?,
?
);
1 change: 1 addition & 0 deletions libpretixsync/src/main/sqldelight/migrations/105.sqm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- Empty migration to set database version to 106
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ CREATE TABLE ReceiptLine (
tax_rate numeric,
tax_rule bigint,
tax_value numeric,
tax_code character varying(255),
type character varying(255),
use_reusable_medium bigint,
variation_id bigint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ CREATE TABLE ReceiptLine (
tax_rate REAL AS BigDecimal,
tax_rule INTEGER,
tax_value REAL AS BigDecimal,
tax_code TEXT,
type TEXT,
use_reusable_medium INTEGER,
variation_id INTEGER,
Expand Down

0 comments on commit 7e9a96e

Please sign in to comment.