Skip to content

Commit 47e8cc7

Browse files
authored
Печать в чеке (#75)
* Update README.md (#74) * Extra print in receipt (#72) * немного кода для печати доп.полей в чеке * првильные преобразования печатных типов * javadoc + consts
1 parent df15c32 commit 47e8cc7

19 files changed

+407
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ allprojects {
1616

1717
```
1818
dependencies {
19-
compile 'com.github.evotor:integration-library:v0.3.+'
19+
compile 'com.github.evotor:integration-library:v0.4.+'
2020
}
2121
```
2222

app/src/main/java/ru/evotor/framework/Utils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ru.evotor.framework;
22

3+
import android.os.Parcelable;
34
import android.support.annotation.NonNull;
5+
import android.support.annotation.Nullable;
46

57
import java.util.ArrayList;
68
import java.util.List;
@@ -34,4 +36,19 @@ public static <T, R> List<R> filterByClass(@NonNull List<T> source, @NonNull Cla
3436
}
3537
return list;
3638
}
39+
40+
@Nullable
41+
public static <T> List<T> convertParcelables(@Nullable Parcelable[] parcelables, @NonNull Class<T> clazz) {
42+
if (parcelables != null) {
43+
List<T> exports = new ArrayList<>();
44+
for (Parcelable parcelable : parcelables) {
45+
if (clazz.isInstance(parcelable)) {
46+
exports.add((T) parcelable);
47+
}
48+
}
49+
return exports;
50+
} else {
51+
return null;
52+
}
53+
}
3754
}

app/src/main/java/ru/evotor/framework/core/action/datamapper/ChangesMapper.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.util.Log
66
import ru.evotor.framework.core.action.event.receipt.changes.IChange
77
import ru.evotor.framework.core.action.event.receipt.changes.UnknownChange
88
import ru.evotor.framework.core.action.event.receipt.changes.position.*
9+
import ru.evotor.framework.core.action.event.receipt.changes.receipt.print_extra.SetPrintExtra
910
import ru.evotor.framework.safeValueOf
1011

1112
object ChangesMapper {
@@ -72,6 +73,7 @@ object ChangesMapper {
7273
IChange.Type.SET_EXTRA -> SetExtra.from(bundle)
7374
IChange.Type.SET_POSITION_PRINT_GROUP -> SetPrintGroup.from(bundle)
7475
IChange.Type.SET_PAYMENT_PURPOSE_PRINT_GROUP -> SetPrintGroup.from(bundle)
76+
IChange.Type.SET_PRINT_EXTRA -> SetPrintExtra.from(bundle)
7577
null, IChange.Type.UNKNOWN -> UnknownChange.from(typeName, bundle)
7678
}
7779
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package ru.evotor.framework.core.action.datamapper
2+
3+
import android.os.Bundle
4+
import ru.evotor.framework.receipt.print_extras.*
5+
import ru.evotor.framework.safeValueOf
6+
7+
object PrintExtraPlaceMapper {
8+
9+
private const val KEY_TYPE = "type"
10+
11+
fun toBundle(printExtraPlace: PrintExtraPlace): Bundle =
12+
Bundle().let {
13+
it.putString(KEY_TYPE, printExtraPlace.placeType.name)
14+
when (printExtraPlace) {
15+
is PrintExtraPlacePrintGroupTop -> PrintExtraPlacePrintGroupTop.addToBundle(it, printExtraPlace)
16+
is PrintExtraPlacePrintGroupHeader -> PrintExtraPlacePrintGroupHeader.addToBundle(it, printExtraPlace)
17+
is PrintExtraPlacePrintGroupSummary -> PrintExtraPlacePrintGroupSummary.addToBundle(it, printExtraPlace)
18+
is PrintExtraPlacePositionFooter -> PrintExtraPlacePositionFooter.addToBundle(it, printExtraPlace)
19+
is PrintExtraPlacePositionAllSubpositionsFooter -> PrintExtraPlacePositionAllSubpositionsFooter.addToBundle(it, printExtraPlace)
20+
}
21+
it
22+
}
23+
24+
25+
fun fromBundle(bundle: Bundle): PrintExtraPlace? =
26+
when (safeValueOf<PrintExtraPlaceType>(bundle.getString(KEY_TYPE))) {
27+
PrintExtraPlaceType.PRINT_GROUP_TOP -> PrintExtraPlacePrintGroupTop.fromBundle(bundle)
28+
PrintExtraPlaceType.PRINT_GROUP_HEADER -> PrintExtraPlacePrintGroupHeader.fromBundle(bundle)
29+
PrintExtraPlaceType.PRINT_GROUP_SUMMARY -> PrintExtraPlacePrintGroupSummary.fromBundle(bundle)
30+
PrintExtraPlaceType.POSITION_FOOTER -> PrintExtraPlacePositionFooter.fromBundle(bundle)
31+
PrintExtraPlaceType.POSITION_ALL_SUBPOSITIONS_FOOTER -> PrintExtraPlacePositionAllSubpositionsFooter.fromBundle(bundle)
32+
else -> null
33+
}
34+
35+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package ru.evotor.framework.core.action.datamapper
2+
3+
import android.graphics.Bitmap
4+
import android.graphics.BitmapFactory
5+
import android.os.Bundle
6+
import android.os.Parcelable
7+
import ru.evotor.devices.commons.printer.printable.IPrintable
8+
import ru.evotor.devices.commons.printer.printable.PrintableBarcode
9+
import ru.evotor.devices.commons.printer.printable.PrintableImage
10+
import ru.evotor.devices.commons.printer.printable.PrintableText
11+
import ru.evotor.framework.safeValueOf
12+
import java.io.ByteArrayOutputStream
13+
14+
15+
object PrintablesMapper {
16+
17+
private const val KEY_PRINTABLE_TYPE = "printableType"
18+
private const val KEY_PRINTABLE_ARRAY = "printablesArray"
19+
private const val KEY_PRINTABL_TEXT = "printableText"
20+
private const val KEY_PRINTABL_BARCODE_TYPE = "printableBarcodeType"
21+
private const val KEY_PRINTABL_IMAGE = "printableImage"
22+
23+
24+
fun toBundle(printables: Array<IPrintable>): Bundle =
25+
Bundle().apply {
26+
val printablesParcelable = arrayOfNulls<Parcelable>(printables.size)
27+
printables.indices.forEach {
28+
printablesParcelable[it] = toBundle(printables.get(it))
29+
}
30+
putParcelableArray(KEY_PRINTABLE_ARRAY, printablesParcelable)
31+
}
32+
33+
private fun toBundle(printable: IPrintable): Bundle =
34+
Bundle().let {
35+
when (printable) {
36+
is PrintableText -> {
37+
it.putString(KEY_PRINTABLE_TYPE, PrintableType.TEXT.name)
38+
it.putString(KEY_PRINTABL_TEXT, printable.text)
39+
}
40+
is PrintableBarcode -> {
41+
it.putString(KEY_PRINTABLE_TYPE, PrintableType.BARCODE.name)
42+
it.putString(KEY_PRINTABL_TEXT, printable.barcodeValue)
43+
it.putString(KEY_PRINTABL_BARCODE_TYPE, printable.barcodeType.name)
44+
}
45+
is PrintableImage -> {
46+
it.putString(KEY_PRINTABLE_TYPE, PrintableType.IMAGE.name)
47+
it.putByteArray(KEY_PRINTABL_IMAGE,
48+
ByteArrayOutputStream().let {
49+
printable.bitmap.compress(Bitmap.CompressFormat.PNG, 100, it)
50+
it.toByteArray()
51+
}
52+
)
53+
}
54+
}
55+
it
56+
}
57+
58+
fun fromBundle(bundle: Bundle): Array<IPrintable>? =
59+
arrayListOf<IPrintable>().let {
60+
list ->
61+
val bundleParcelables: Array<Parcelable> = bundle.getParcelableArray(KEY_PRINTABLE_ARRAY)
62+
bundleParcelables.forEach {
63+
bundle ->
64+
if (bundle is Bundle) {
65+
singleFromBundle(bundle)?.let { list.add(it) }
66+
}
67+
}
68+
list.toTypedArray()
69+
}
70+
71+
private fun singleFromBundle(bundle: Bundle): IPrintable? =
72+
when (safeValueOf<PrintableType>(bundle.getString(KEY_PRINTABLE_TYPE), null)) {
73+
PrintableType.TEXT -> PrintableText(
74+
bundle.getString(KEY_PRINTABL_TEXT)
75+
)
76+
PrintableType.BARCODE -> PrintableBarcode(
77+
bundle.getString(KEY_PRINTABL_TEXT),
78+
safeValueOf<PrintableBarcode.BarcodeType>(bundle.getString(KEY_PRINTABL_BARCODE_TYPE), PrintableBarcode.BarcodeType.CODE39)
79+
)
80+
PrintableType.IMAGE ->
81+
bundle.getByteArray(KEY_PRINTABL_IMAGE).let {
82+
PrintableImage(
83+
BitmapFactory.decodeByteArray(it, 0, it.size)
84+
)
85+
}
86+
null -> null
87+
}
88+
89+
enum class PrintableType {
90+
TEXT, BARCODE, IMAGE
91+
}
92+
}

app/src/main/java/ru/evotor/framework/core/action/event/receipt/changes/IChange.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface IChange : IBundlable {
1212
SET_EXTRA,
1313
SET_POSITION_PRINT_GROUP,
1414
SET_PAYMENT_PURPOSE_PRINT_GROUP,
15+
SET_PRINT_EXTRA,
1516
UNKNOWN
1617
}
1718
}

app/src/main/java/ru/evotor/framework/core/action/event/receipt/changes/receipt/SetPrintGroup.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ data class SetPrintGroup(val printGroup: PrintGroup?, val paymentPurposeIds: Lis
2424
}
2525
}
2626

27-
override fun getType(): IChange.Type {
28-
return IChange.Type.SET_PAYMENT_PURPOSE_PRINT_GROUP
29-
}
27+
override fun getType() = IChange.Type.SET_PAYMENT_PURPOSE_PRINT_GROUP
3028

3129
companion object {
3230
const val KEY_PRINT_GROUP = "printGroup"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ru.evotor.framework.core.action.event.receipt.changes.receipt.print_extra
2+
3+
import android.os.Bundle
4+
import ru.evotor.devices.commons.printer.printable.IPrintable
5+
import ru.evotor.framework.core.action.datamapper.PrintExtraPlaceMapper
6+
import ru.evotor.framework.core.action.datamapper.PrintablesMapper
7+
import ru.evotor.framework.core.action.event.receipt.changes.IChange
8+
import ru.evotor.framework.receipt.print_extras.PrintExtraPlace
9+
10+
class SetPrintExtra(
11+
val printPlace: PrintExtraPlace,
12+
val printables: Array<IPrintable>
13+
) : IChange {
14+
15+
override fun getType() = IChange.Type.SET_PRINT_EXTRA
16+
17+
override fun toBundle(): Bundle =
18+
Bundle().apply {
19+
putBundle(KEY_PRINT_PLACE, PrintExtraPlaceMapper.toBundle(printPlace))
20+
putBundle(KEY_PRINTABLES, PrintablesMapper.toBundle(printables))
21+
}
22+
23+
companion object {
24+
25+
const val KEY_PRINT_PLACE = "printPlace"
26+
const val KEY_PRINTABLES = "printables"
27+
28+
@JvmStatic
29+
fun from(bundle: Bundle?): SetPrintExtra? {
30+
bundle ?: return null
31+
32+
val printPlace = PrintExtraPlaceMapper.fromBundle(bundle.getBundle(KEY_PRINT_PLACE))
33+
val printables = PrintablesMapper.fromBundle(bundle.getBundle(KEY_PRINTABLES))
34+
35+
if (printPlace == null || printables == null) {
36+
return null
37+
}
38+
39+
return SetPrintExtra(printPlace, printables)
40+
}
41+
}
42+
43+
}

app/src/main/java/ru/evotor/framework/core/action/event/receipt/merges/PositionsMergeEvent.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class PositionsMergeEvent(val receiptUuid: String, val merges: ArrayList<Positio
1717
}
1818

1919
companion object {
20-
val NAME_SELL_RECEIPT = "evo.v2.receipt.sell.mergingPositions"
21-
val NAME_PAYBACK_RECEIPT = "evo.v2.receipt.payback.mergingPositions"
20+
const val NAME_SELL_RECEIPT = "evo.v2.receipt.sell.mergingPositions"
21+
const val NAME_PAYBACK_RECEIPT = "evo.v2.receipt.payback.mergingPositions"
2222
private val KEY_RECEIPT_UUID = "receiptUuid"
2323
private val KEY_MERGES = "merges"
2424

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ru.evotor.framework.core.action.event.receipt.print_extra
2+
3+
import android.os.Bundle
4+
import ru.evotor.IBundlable
5+
6+
class PrintExtraRequiredEvent() : IBundlable {
7+
8+
override fun toBundle(): Bundle {
9+
return Bundle()
10+
}
11+
12+
companion object {
13+
const val NAME_PERMISSION = "ru.evotor.permission.receipt.printExtra.SET"
14+
const val NAME_SELL_RECEIPT = "evo.v2.receipt.sell.printExtra.REQUIRED"
15+
const val NAME_PAYBACK_RECEIPT = "evo.v2.receipt.payback.printExtra.REQUIRED"
16+
17+
fun create(bundle: Bundle?): PrintExtraRequiredEvent? {
18+
if (bundle == null) {
19+
return null
20+
}
21+
22+
return PrintExtraRequiredEvent()
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)