summaryrefslogtreecommitdiff
path: root/app/src/main/java/net/taler/merchantpos/order/Definitions.kt
blob: ce2e46453b7035292e519d581167529e5e550c34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package net.taler.merchantpos.order

import com.fasterxml.jackson.annotation.JsonProperty
import net.taler.merchantpos.Amount

data class Category(
    val id: Int,
    val name: String
) {
    var selected: Boolean = false
}

interface Product {
    val id: String
    val description: String
    val price: String
    val location: String?
}

data class ConfigProduct(
    @JsonProperty("product_id")
    override val id: String,
    override val description: String,
    override val price: String,
    @JsonProperty("delivery_location")
    override val location: String?,
    val categories: List<Int>
) : Product {
    val priceAsDouble by lazy { Amount.fromString(price).amount.toDouble() }
}

data class ContractProduct(
    @JsonProperty("product_id")
    override val id: String,
    override val description: String,
    override val price: String,
    @JsonProperty("delivery_location")
    override val location: String?,
    val quantity: Int
) : Product {
    constructor(product: ConfigProduct, quantity: Int) : this(
        product.id,
        product.description,
        product.price,
        product.location,
        quantity
    )
}

data class Order(val availableCategories: Map<Int, Category>) {
    val products = HashMap<ConfigProduct, Int>()
    val summary: String
        get() {
            val categories = HashMap<Category, Int>()
            products.forEach { (product, quantity) ->
                val categoryId = product.categories[0]
                val category = availableCategories.getValue(categoryId)
                val oldQuantity = categories[category] ?: 0
                categories[category] = oldQuantity + quantity
            }
            return categories.map { (category, quantity) ->
                "$quantity x ${category.name}"
            }.joinToString()
        }
    val total: Double
        get() {
            var total = 0.0
            products.forEach { (product, quantity) ->
                val price = product.priceAsDouble
                total += price * quantity
            }
            return total
        }
    val totalAsString: String
        get() = String.format("%.2f", total)

    operator fun plus(product: ConfigProduct): Order {
        products[product] = (products[product] ?: 0) + 1
        return this
    }

    operator fun minus(product: ConfigProduct): Order {
        var quantity = products[product] ?: throw IllegalStateException()
        quantity -= 1
        if (quantity > 0) products[product] = quantity
        else products.remove(product)
        return this
    }
}

typealias OrderLine = Pair<ConfigProduct, Int>