Skip to content

Commit b65029b

Browse files
committed
add reference units + use units field for articles marked with "piece"
1 parent 0b5aab1 commit b65029b

File tree

7 files changed

+147
-27
lines changed

7 files changed

+147
-27
lines changed

core/src/main/java/io/snabble/sdk/Events.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,26 @@ private PayloadCart getPayloadCart() {
208208
Product product = shoppingCart.getProduct(i);
209209
int quantity = shoppingCart.getQuantity(i);
210210

211-
payloadCart.items[i] = new PayloadCartItem();
212-
payloadCart.items[i].sku = String.valueOf(product.getSku());
213-
payloadCart.items[i].scannedCode = shoppingCart.getScannedCode(i);
214-
payloadCart.items[i].amount = quantity;
215-
payloadCart.items[i].units = shoppingCart.getEmbeddedUnits(i);
216-
payloadCart.items[i].weight = shoppingCart.getEmbeddedWeight(i);
217-
payloadCart.items[i].price = shoppingCart.getEmbeddedPrice(i);
211+
PayloadCartItem item = new PayloadCartItem();
212+
213+
item.sku = String.valueOf(product.getSku());
214+
item.scannedCode = shoppingCart.getScannedCode(i);
215+
item.amount = quantity;
216+
item.units = shoppingCart.getEmbeddedUnits(i);
217+
item.weight = shoppingCart.getEmbeddedWeight(i);
218+
item.price = shoppingCart.getEmbeddedPrice(i);
218219

219220
if (product.getType() == Product.Type.UserWeighed) {
220-
payloadCart.items[i].amount = 1;
221-
payloadCart.items[i].weight = quantity;
221+
item.amount = 1;
222+
item.weight = quantity;
223+
}
224+
225+
if (item.units == null && product.getReferenceUnit() == Unit.PIECE) {
226+
item.amount = 1;
227+
item.units = quantity;
222228
}
229+
230+
payloadCart.items[i] = item;
223231
}
224232

225233
return payloadCart;

core/src/main/java/io/snabble/sdk/Product.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public static SaleRestriction fromDatabaseField(long dbType, long value) {
113113
private String subtitle;
114114
private String basePrice;
115115
private SaleRestriction saleRestriction = SaleRestriction.NONE;
116+
private Unit referenceUnit;
116117
private Map<String, String> transmissionCodes;
117118
private boolean saleStop;
118119

@@ -223,6 +224,10 @@ public SaleRestriction getSaleRestriction() {
223224
return saleRestriction;
224225
}
225226

227+
public Unit getReferenceUnit() {
228+
return referenceUnit;
229+
}
230+
226231
/**
227232
* @return returns true if this product should not be available for sale anymore.
228233
*/
@@ -401,6 +406,11 @@ public Builder setSaleStop(boolean saleStop) {
401406
return this;
402407
}
403408

409+
public Builder setReferenceUnit(Unit referenceUnit) {
410+
product.referenceUnit = referenceUnit;
411+
return this;
412+
}
413+
404414
public Builder addTransmissionCode(String fromCode, String transmissionCode) {
405415
if (product.transmissionCodes == null) {
406416
product.transmissionCodes = new HashMap<>();

core/src/main/java/io/snabble/sdk/ProductApi.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ private static class ApiWeighing {
4545
private String[] weighedItemIds;
4646
private boolean weighByCustomer;
4747
private String encodingUnit;
48+
private String referenceUnit;
4849
}
4950

5051
private static class ApiProductGroup {
@@ -210,7 +211,7 @@ public void findByCode(final String code, final OnProductAvailableListener produ
210211
return;
211212
}
212213

213-
url = url.replace("{responseCode}", code);
214+
url = url.replace("{code}", code);
214215
url = appendShopId(url);
215216

216217
get(url, productAvailableListener);
@@ -475,10 +476,13 @@ private Product toProduct(ApiProduct apiProduct, Product depositProduct, Product
475476
if (apiProduct.weighing != null) {
476477
builder.setWeighedItemIds(apiProduct.weighing.weighedItemIds);
477478

479+
Unit referenceUnit = Unit.fromString(apiProduct.weighing.referenceUnit);
480+
builder.setReferenceUnit(referenceUnit);
481+
478482
if (apiProduct.weighing.weighByCustomer) {
479483
builder.setType(Product.Type.UserWeighed);
480484
} else {
481-
if ("piece".equals(apiProduct.weighing.encodingUnit)) {
485+
if (referenceUnit == Unit.PIECE) {
482486
builder.setType(Product.Type.Article);
483487
} else {
484488
builder.setType(Product.Type.PreWeighed);

core/src/main/java/io/snabble/sdk/ProductDatabase.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,20 @@ public Product productAtCursor(Cursor cursor) {
792792
}
793793
}
794794

795+
if (schemaVersionMajor >= 1 && schemaVersionMinor >= 16) {
796+
String referenceUnit = cursor.getString(13);
797+
if (referenceUnit != null) {
798+
Unit unit = Unit.fromString(referenceUnit);
799+
builder.setReferenceUnit(unit);
800+
801+
if (unit == Unit.PIECE) {
802+
builder.setType(Product.Type.Article);
803+
} else {
804+
builder.setType(Product.Type.PreWeighed);
805+
}
806+
}
807+
}
808+
795809
Shop shop = project.getCheckedInShop();
796810

797811
if(!queryPrice(builder, sku, shop)) {
@@ -878,6 +892,10 @@ private String productSqlString(String appendSql, boolean distinct) {
878892
sql += ",(SELECT group_concat(ifnull(s.transmissionCode, \"\")) FROM scannableCodes s WHERE s.sku = p.sku)";
879893
}
880894

895+
if (schemaVersionMajor >= 1 && schemaVersionMinor >= 16) {
896+
sql += ",p.referenceUnit";
897+
}
898+
881899
sql += " FROM products p ";
882900
sql += appendSql;
883901

core/src/main/java/io/snabble/sdk/ShoppingCart.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,25 @@ public void insert(Product product, int index, int quantity) {
106106
public void insert(Product product, int index, int quantity, ScannableCode scannedCode, boolean isZeroAmountProduct) {
107107
Entry e = getEntryBySku(product.getSku());
108108

109-
if (e == null || scannedCode.hasUnitData()
110-
|| scannedCode.hasPriceData()
111-
|| product.getType() == Product.Type.UserWeighed
112-
|| product.getType() == Product.Type.PreWeighed) {
113-
if (quantity > 0) {
114-
Entry entry = new Entry(product, quantity);
115-
setScannedCodeForEntry(entry, scannedCode);
116-
entry.isZeroAmountProduct = isZeroAmountProduct;
117-
addEntry(entry, index);
118-
}
119-
} else {
120-
setEntryQuantity(e, e.quantity + quantity);
109+
// if (e == null || scannedCode.hasUnitData()
110+
// || scannedCode.hasPriceData()
111+
// || product.getType() == Product.Type.UserWeighed
112+
// || product.getType() == Product.Type.PreWeighed) {
113+
// if (quantity > 0) {
114+
// Entry entry = new Entry(product, quantity);
115+
// setScannedCodeForEntry(entry, scannedCode);
116+
// entry.isZeroAmountProduct = isZeroAmountProduct;
117+
// addEntry(entry, index);
118+
// }
119+
// } else {
120+
// setEntryQuantity(e, e.quantity + quantity);
121+
// }
122+
123+
if (quantity > 0) {
124+
Entry entry = new Entry(product, quantity);
125+
setScannedCodeForEntry(entry, scannedCode);
126+
entry.isZeroAmountProduct = isZeroAmountProduct;
127+
addEntry(entry, index);
121128
}
122129
}
123130

@@ -149,7 +156,7 @@ public void setQuantity(Product product, int quantity, ScannableCode scannedCode
149156
if (product.getType() == Product.Type.Article) {
150157
Entry e = getEntryBySku(product.getSku());
151158

152-
if (e != null) {
159+
if (e != null && product.getReferenceUnit() != Unit.PIECE) {
153160
// update product for changing prices
154161
e.product = product;
155162

@@ -472,7 +479,8 @@ public int getTotalQuantity() {
472479
for (Entry e : items) {
473480
Product product = e.product;
474481
if (product.getType() == Product.Type.UserWeighed
475-
|| product.getType() == Product.Type.PreWeighed) {
482+
|| product.getType() == Product.Type.PreWeighed
483+
|| product.getReferenceUnit() == Unit.PIECE) {
476484
sum += 1;
477485
} else {
478486
sum += e.quantity;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.snabble.sdk;
2+
3+
public enum Unit {
4+
MILLILITER("ml"),
5+
DECILITER("dl"),
6+
LITER("l"),
7+
CUBIC_METER("m³"),
8+
CUBIC_CENTIMETER("cm³"),
9+
SQUARE_METER("m²"),
10+
SQUARE_CENTIMETER("cm²"),
11+
MILLIMETER("mm"),
12+
CENTIMETER("cm"),
13+
METER("m"),
14+
KILOGRAM("kg"),
15+
GRAM("g"),
16+
TONNE("t"),
17+
PIECE(""),
18+
PRICE("");
19+
20+
private String displayValue;
21+
22+
Unit(String displayValue) {
23+
this.displayValue = displayValue;
24+
}
25+
26+
public static Unit fromString(String value) {
27+
switch (value) {
28+
case "ml": return MILLILITER;
29+
case "dl": return DECILITER;
30+
case "l": return LITER;
31+
case "m3": return CUBIC_METER;
32+
case "cm3": return CUBIC_CENTIMETER;
33+
case "m2": return SQUARE_METER;
34+
case "cm2": return SQUARE_CENTIMETER;
35+
case "mm": return MILLIMETER;
36+
case "cm": return CENTIMETER;
37+
case "m": return METER;
38+
case "kg": return KILOGRAM;
39+
case "g": return GRAM;
40+
case "t": return TONNE;
41+
case "piece": return PIECE;
42+
case "price": return PRICE;
43+
}
44+
45+
return null;
46+
}
47+
48+
public String getDisplayValue() {
49+
return displayValue;
50+
}
51+
52+
public boolean isMass() {
53+
return this == KILOGRAM || this == GRAM || this == TONNE;
54+
}
55+
56+
public boolean isVolume() {
57+
return this == MILLILITER || this == DECILITER || this == LITER;
58+
}
59+
60+
public boolean isArea() {
61+
return this == SQUARE_CENTIMETER || this == SQUARE_METER;
62+
}
63+
64+
public boolean isCapacity() {
65+
return this == CUBIC_METER || this == CUBIC_CENTIMETER;
66+
}
67+
}

ui/src/main/java/io/snabble/sdk/ui/scanner/ProductConfirmationDialog.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.snabble.sdk.Product;
2929
import io.snabble.sdk.Project;
3030
import io.snabble.sdk.ShoppingCart;
31+
import io.snabble.sdk.Unit;
3132
import io.snabble.sdk.codes.EAN13;
3233
import io.snabble.sdk.codes.ScannableCode;
3334
import io.snabble.sdk.PriceFormatter;
@@ -109,7 +110,6 @@ public void show(Product newProduct, ScannableCode scannedCode) {
109110
quantity.clearFocus();
110111

111112
Product.Type type = product.getType();
112-
113113
int cartQuantity = shoppingCart.getQuantity(product);
114114

115115
if (scannedCode.hasEmbeddedData()) {
@@ -143,7 +143,12 @@ public void show(Product newProduct, ScannableCode scannedCode) {
143143
}
144144
} else if (type == Product.Type.Article) {
145145
quantityAnnotation.setVisibility(View.GONE);
146-
quantity.setText(String.valueOf(Math.min(ShoppingCart.MAX_QUANTITY, cartQuantity + 1)));
146+
147+
if (product.getReferenceUnit() == Unit.PIECE) {
148+
quantity.setText("1");
149+
} else {
150+
quantity.setText(String.valueOf(Math.min(ShoppingCart.MAX_QUANTITY, cartQuantity + 1)));
151+
}
147152
} else if (type == Product.Type.UserWeighed) {
148153
quantityAnnotation.setVisibility(View.VISIBLE);
149154
quantityAnnotation.setText("g");

0 commit comments

Comments
 (0)